preg_match to validate form
Posted by ontheplains (ontheplains), 6 May 2007i have never before used the preg_match feature, though i will have to use it for this project. i am making a madlib on my page and the values that people input into the fields need to meet a certain criteria: i need to create a function that will check the 4 text inputs. each one must consist of a single word (i.e. no spaces and no extra punctuation such as periods or commas). the first letter may be capitalized (but not other letters). dashes should be allowed after the first letter, provided that you do not end with a dash. each word must be at least 2 characters long.
the code for the input goes as such:
i've never used preg_match before, so i was confused as to how to look for and meet these specific criteria. thank you SO MUCH for any and all help.
Posted by admin (Graham Ellis), 6 May 2007To write the regular expression, you should re-phrase what you're looking for describing it from left to right and giving a count of how many times each of the elements occurs. In your example, I would write:
* Starts With
* Upper or a lower case letter ( 1 thereof)
* Lower case letters or dashes ( 0 or more thereof)
* Lower case letter (1 thereof)
And that translates to:
or as the string for preg_match"
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho