| |||||||||||
| |||||||||||
preg_match to validate form Posted by ontheplains (ontheplains), 6 May 2007 i 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: Code:
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. ~ontheplains Posted by admin (Graham Ellis), 6 May 2007 To 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) * Ends And that translates to: ^ [A-Za-z] [-a-z]* [a-z] $ or as the string for preg_match" '/^[A-Za-z][-a-z]*[a-z]$/' This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |