Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
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:

<p class="centerwindow">
     <form name="form1" action="madlib.php">

     <p class="centerwindow">I want to do madlib number:<input type="radio" checked="checked" name="madlib" onClick="document.form1.noun3.disabled=false;document.form1.verb1.disabled=false" value="1" />1<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=false;document.form1.verb1.disabled=false" value="2" />2<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=false;document.form1.verb1.disabled=false" value="3" />3<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=true;document.form1.verb1.disabled=true;document.form1.noun3.value='';document.form1.verb1.value='';" value="4" />4

<ol class="centerwindow">
<li> A noun (plural):</li>&nbsp;&nbsp;<input type="text" name="noun1" />
<li> A noun:</li>&nbsp;&nbsp;<input type="text" name="noun2" />
<li> A noun:</li>&nbsp;&nbsp;<input type="text" name="noun3" />
<li> A verb (ending in 'ing'):</li>&nbsp;&nbsp;<input type="text" name="verb1" />

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 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:


or as the string for preg_match"


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho