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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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:
<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" />
</ol>


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho