2

I'm setting up a custom php form validation used in conjunction with wordpress contact form 7 plugin, I've set up a validation rule as the text field, I only want people to type 1 word (i.e. no spaces) and for that one word to begin with the letter s.
The letter s validation works just fine but while this works, the no spaces (one word) validation doesn't seem to be working?
php:

if($name == 'FirstField') {
        $FirstField = $_POST['FirstField'];

        if($FirstField != '') {
            if (!preg_match("/(^[^s]|\s)/i",$FirstField)){
                $result['valid'] = true;
            } else {
                $result['valid'] = false;
                $result['reason'][$name] = 'Invalid Entry.';
            }
        }
    }

Any suggestions would be greatly appreciated!

2
  • When I tested your regex it seems to do what you want. Can you give an example of a value that does not give your expected result? Commented Dec 19, 2013 at 20:54
  • As a side note, you can always test your regular expressions here... rubular.com/r/CnxN4oJub2 Commented Dec 19, 2013 at 21:35

3 Answers 3

2

Test:

if($name == 'FirstField') { $FirstField = $_POST['FirstField'];

    if($FirstField != '') {
        if (!preg_match("/(^[^s]|\+s)/i",$FirstField)){
            $result['valid'] = true;
        } else {
            $result['valid'] = false;
            $result['reason'][$name] = 'Invalid Entry.';
        }
    }
}

UPDATED:

if($name == 'FirstField') { $FirstField = $_POST['FirstField'];

    if($FirstField != '' && isset($FirstField) ) {
        $FirstField = explode(' ', $FirstField); //Get only first word
        if (!preg_match("/(^[^s]|\+s)/i",$FirstField[0])){
            $result['valid'] = true;
        } else {
            $result['valid'] = false;
            $result['reason'][$name] = 'Invalid Entry.';
        }
    }
}

FIXED ERROR last:

replace:

if($FirstField != '' && isset($FirstField)

by:

if($FirstField != '' && isset($FirstField) )

I hope help you.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your suggestion, still doesn't seem to be working though. the s validation is working fine, the no spaces one though, still isn't :(
still no luck with that, there's a syntax error in there too, missing a closing bracket ;) thanks for your help though…just still no luck :(
if($FirstField != '' && isset($FirstField) ) ... The problem is closet ")" the condition statement "if" ;-)
even with the syntax correction is still doesn't seem to work, again the s validation is still working, the spaces one is still a no go though, I can't figure it out :(
but do not worry anyway with denial "!" in condition and works. My is working in my test site.
|
0

I believe this should work:

preg_match("/^s[a-z]*$/i", $FirstField) 

Assuming that your word consists of only letters.

Comments

0

Starting with 's' (case insensitive), no spaces: preg_match('/^s[^ ]*$/i', $string)

Starting with 's' (case insensitive), no white-space (spaces, tabs, etc): preg_match('/^s\S*$/i', $string)

edit

You might also be interested in \w, which matches "word characters" (A-Z, a-z, 0-9, _): preg_match('/^s\w*$/i', $string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.