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.disabl ed=false" value="1" />1<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=false;document.form1.verb1.disabl ed=false" value="2" />2<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=false;document.form1.verb1.disabl ed=false" value="3" />3<input type="radio" name="madlib" onClick="document.form1.noun3.disabled=true;document.form1.verb1.disable d=true;document.form1.noun3.value='';document.form1.verb1.value='';" value="4" />4 <ol class="centerwindow"> <li> A noun (plural):</li> <input type="text" name="noun1" /> <li> A noun:</li> <input type="text" name="noun2" /> <li> A noun:</li> <input type="text" name="noun3" /> <li> A verb (ending in 'ing'):</li> <input type="text" name="verb1" /> </ol> Code (markup):
^^ No need for the {1}, it will only match one in your case anyway. And you have to escape the w. But the \w matches under scores, upper case letters and numbers, which he doesn't want. Try this: preg_match('/^[A-Za-z][a-z-]+$/', $text); PHP:
nico, the [a-z-]+$ will allow a dash at the end of the word. so you probably want to make the [a-z-] one * (can exist between 0 and multiple times) then followed by a mandatory [a-z] which should end the word.
Good point, didn't think of that. Also, I didn't think that it would allow multiple dashes, or only dashes after the first character. $text = preg_replace('/-{2,}/', '-', $text); preg_match('/^[A-Za-z][a-z-]*[a-z]+$/', $text); PHP: