Note that there are some explanatory texts on larger screens.

plurals
  1. POphp: validate if field starts with certain character
    primarykey
    data
    text
    <p>I'm using the Contact Form 7 plugin on wordpress to collect data inputted in the fields, I'm now looking to set up some validation rules using this neat extension: <a href="http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/" rel="nofollow">http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/</a> <br> What I'm after is to only allow one word only in the text field (i.e. no whitespace) and this one word has to begin with the letter 'r' (not case sensitive).<br> I've written the no white space rule as follows:</p> <pre><code>//whitespace if($name == 'WhiteSpace') { $WhiteSpace = $_POST['WhiteSpace']; if($WhiteSpace != '') { if (!preg_match('/\s/',$WhiteSpace)){ $result['valid'] = true; } else { $result['valid'] = false; $result['reason'][$name] = 'Invalid Entry.'; } } } </code></pre> <p>Is it possible to incorporate the second rule into this also? So no whitespace, and the word must begin with the letter 'r'? Any suggestions would be greatly appreciated!</p> <hr> <p>EDIT: seems <code>core1024</code> answer does work, but only one of them:</p> <pre><code> //FirstField if($name == 'FirstField') { $FirstField = $_POST['FirstField']; if($FirstField != '') { if (!preg_match("/(^[^a]|\s)/i",$FirstField)){ $result['valid'] = true; } else { $result['valid'] = false; $result['reason'][$name] = 'Invalid Entry.'; } } } //__________________________________________________________________________________________________ //SecondField if($name == 'SecondField') { $SecondField = $_POST['SecondField']; if($SecondField != '') { if (!preg_match("/(^[^r]|\s)/i", $SecondField)) { $result['valid'] = true; } else { $result['valid'] = false; $result['reason'][$name] = 'Invalid Entry.'; } } } </code></pre> <p>I want to use this code twice, once to validate the first character being <code>a</code> on one field the second instance with the first character being <code>r</code> on another field. But it only seems the <code>SecondField</code> validation rule is working.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload