skaterdav85 Posted August 7, 2009 Share Posted August 7, 2009 I want to check if a form field's value is between 1-10. I can do [1-9] but then its missing 10.and if i do [1-9][0], then someone can enter in 20, 30, 40 etc.Any way of doing this without doing an if statement? Link to comment Share on other sites More sharing options...
Ingolme Posted August 7, 2009 Share Posted August 7, 2009 In regular expressions, [0-9] doesn't mean a number from 0 to 9, it means any character between 0 and 9.Regular expressions don't recognize numeric values, only strings.If you want to check for 1 to 9 or 10, then use a vertical bar: ([1-9]|10) And if you don't want to create a back reference: (?:[1-9]|10) But for something so simple as this, I don't even recommend a regular expression. Just check the value, because regular expressions take more processing. if($_POST['field'] < 1 || $_POST['field'] > 10) { echo "The field must have a value between 1 and 10";} Link to comment Share on other sites More sharing options...
skaterdav85 Posted August 7, 2009 Author Share Posted August 7, 2009 In regular expressions, [0-9] doesn't mean a number from 0 to 9, it means any character between 0 and 9.Regular expressions don't recognize numeric values, only strings.so what is the order of characters then? a-z then 0-9? what about capital A? Link to comment Share on other sites More sharing options...
Ingolme Posted August 7, 2009 Share Posted August 7, 2009 so what is the order of characters then? a-z then 0-9? what about capital A?In a regular expression, a series of characters between square brackets indicate that one single character of the list can be selected:This regular expression matches both "grey" and "gray," but not "graey":/gr[ae]y/ Link to comment Share on other sites More sharing options...
skaterdav85 Posted August 7, 2009 Author Share Posted August 7, 2009 what about this: [a-zA-Z0-9] ... Could this be rewritten like [a-9]? Link to comment Share on other sites More sharing options...
Ingolme Posted August 7, 2009 Share Posted August 7, 2009 what about this: [a-zA-Z0-9] ... Could this be rewritten like [a-9]?No, because 9 goes before a in the ASCII system.You can check this table to see which characters can have hyphens between them:http://asciitable.com/If you write [Z-a] it will allow the following characters: Z[\]^_`a Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.