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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho