preg_match to validate form

Discussion in 'PHP' started by ontheplains, May 5, 2007.

  1. #1
    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>&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>
    Code (markup):
     
    ontheplains, May 5, 2007 IP
  2. dzysyak

    dzysyak Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think something like this:

    
    preg_match("/\b[A-Z]{1}w+\b/", $text);
    
    PHP:
     
    dzysyak, May 5, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    ^^ 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_swd, May 6, 2007 IP
  4. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #4
    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.
     
    legend2, May 7, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    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:
     
    nico_swd, May 7, 2007 IP