I want to create a program that validate user input. I have found a Regex that validates if the input is correct or not. The problem is that the program allows the user to enter extra characters which are not part of the correct input. For instance the correct input is 23km, it allows the user to put something like 23rt that's what I want to eliminate. Here is my code:
QRegExp r1("\\d{1,}(km|hm|dam|m|dm|сm) \\d{1,2}(km|hm|dam|m|dm|сm) x \\d{1,}(km|hm|dam|m|dm|сm) \\d{1,2}(km|hm|dam|m|dm|сm)");
if(input.isEmpty()) return QValidator::Intermediate;
if(r1.exactMatch(input)) return QValidator::Acceptable; else
return QValidator::Intermediate;
I tried return QValidator::Invalid; at the end and it does not take inputs at all.
I want to make something in form of: (23km 3m X 1km 4m).
I think you might be making a mistake in getting the input string that is being passed to this function. std::cin automatically breaks at spaces, make sure you are using std::getline. The below code appears to work for me.