Help me with regular expressions. I need to check the text on the hour and minute. That is the first case, the text can be from 0 to 12. In the second case, the text can be from 1 to 60.
this is my code:
var hourRegEx = /^([0-9]{2})$/; //You can fix this line of code?
$(document).ready(
function(){
$('form.form').submit(function(){
if( $('input.hour').val().match(hourRegEx) ){
return true;
}
return false;
});
});
In my case, the code says that, for example 52, too, the correct answer
Help me with regular expressions. I need to check the text on the hour and minute. That is the first case, the text can be from 0 to 12. In the second case, the text can be from 1 to 60.
this is my code:
var hourRegEx = /^([0-9]{2})$/; //You can fix this line of code?
$(document).ready(
function(){
$('form.form').submit(function(){
if( $('input.hour').val().match(hourRegEx) ){
return true;
}
return false;
});
});
In my case, the code says that, for example 52, too, the correct answer
Share Improve this question asked Jan 10, 2011 at 12:09 Zhasulan BerdibekovZhasulan Berdibekov 1,0873 gold badges20 silver badges39 bronze badges 2- 2 Unless you live in some alternate reality, hours can only be 0 to 11. – Jamiec Commented Jan 10, 2011 at 12:12
- I do not know much about regular expressions – Zhasulan Berdibekov Commented Jan 10, 2011 at 12:12
8 Answers
Reset to default 3I would do this with parseInt
and numeric checking:
var hour = parseInt($('input.hour').val(), 10);
if((hour >= 0) && (hour <= 11)){
return true;
}
If what you are inherently doing is paring numbers you really shouldn't use a regex
I would do this:
var numericRegEx = /^[0-9]+$/;
$(document).ready(
function(){
$('form.form').submit(function(){
var hourVal = $('input.hour').val();
if( hourVal.match(numericRegEx) && parseInt(hourVal) <= 11){
return true;
}
return false;
});
});
This may be overly plicated; if I was doing this I would just use parseInt, but the original code would return false for values such as "11blah" so some regex functionality is still used to check the entire string is an integer.
Why not convert it to integer and use <
? Regex is not a substitution for integer arithmetics.
Use: /^([0-9]|10|11|12)$/
. It's short enough, and very clear :)
Edit: or, if @Jamiec is right and you're mistaken about the numbers, /^([0-9]|10|11)$/
For minutes, use: /^([0-9]|[1-5][0-9])$/
.
Edit 2: ah wait, 1 to 60. Use this:
/^([1-9]|[1-5][0-9]|60)$/
and for hours 1-12, if you need it:
/^([1-9]|10|11|12)$/
or /^([1-9]|1[0-2])$/
The RegEx for testing a number between 0 and 12 would be along the lines of ^([0-9]|1[012])$
and for the minutes: ^[1-9]|[2-5][0-9]$
.
I wouldn't remend it though. Personally, I'd use parseInt to get the value as a number. You can check it's a valid number because parseInt
will return NaN
if it isn't. Then you can do your range check.
var hourVal = parseInt($('input.hour').val(),10),
minVal = parseInt($('input.minute').val(),10);
if(hourVal && hourVal >=0 and hourVal <= 12){
// hour valid
}
if(minVal && minVal >=1 and minVal <= 60){
// min valid
}
This should handle also cases where the user inputs 00-09:
/^(0?[0-9]|1[0-2])$/
This:
[1-9]|1[0-2]
will match the hour ( 1 .. 12 )
[1-9]|[1-5][0-9]|60
will match the minutes ( 1 .. 60 )
if you want to match 0 .. 11 and 0 .. 59 do this
[0-9]|1[0-1]
will match the hour ( 0 .. 11 )
[0-9]|[1-5][0-9]
will match the minutes ( 0 .. 59 )
if you want to match 00 .. 11 and 00 .. 59 do this
0[0-9]|10|11
will match the hour ( 00 .. 11 )
[0-5][0-9]
will match the minutes ( 00 .. 59 )
Using regex is not viable for this case. You should pare integer instead, because '1', '01', '001' are valid too.
i remand this kind of snippet
val = parseInt($('input.hour').val());
if (val >= 0 && val <= 12)
// is valid ...