Professional Web Applications Themes

preg_match help please - PHP Development

Hi I want to see that a user is submiting a form field in the correct manner. So I decided to use preg_match to verify their input. The pattern I am trying to match is; Firstname Secondname I am not bothered about character length just yet (but advise on this would be appreciated if it can be done with a regular expression - saves extra checking code), but I would like case sensitity. The code I haev so far, which doesn't seem to work is; $un = $_REQUEST['name']; $exp = '/^\b[a-zA-Z] [a-zA-Z]$/'; if (preg_match($exp, $un)) { //hasthismatched? echo"<h2>matched</h2>"; } Help ...

  1. #1

    Default preg_match help please

    Hi

    I want to see that a user is submiting a form field in the correct manner.
    So I decided to use preg_match to verify their input.

    The pattern I am trying to match is;

    Firstname Secondname

    I am not bothered about character length just yet (but advise on this would
    be appreciated if it can be done with a regular expression - saves extra
    checking code), but I would like case sensitity.

    The code I haev so far, which doesn't seem to work is;

    $un = $_REQUEST['name'];
    $exp = '/^\b[a-zA-Z] [a-zA-Z]$/';

    if (preg_match($exp, $un)) {
    //hasthismatched?
    echo"<h2>matched</h2>";
    }

    Help would be greatly appreciated

    Thanks
    Steve
    Steve Guest

  2. #2

    Default Re: preg_match help please

     
     

    You should check the manual pages for the PHP pattern matching
    functions and follow some of the references quoted in the user
    comments, as there are some very good tutorials out there.

    As a starter, the pattern "[a-zA-Z]" matches exactly one character in
    the set specified. You probably want to match more than one, unless
    your users have very terse names...

    See also <http://www.hudzilla.org/phpbook/read.php/4_8_0>

    ---
    Steve

    Steve Guest

  3. #3

    Default Re: preg_match help please

    Steve wrote:
     

    Sorry for the stupid question, but why don't you simply use two input
    fields - one for firstname and one for secondname?

    I don't see a reason why you would want to use reg exps in this case.

    Cheers,
    Nick
    Nick Guest

Similar Threads

  1. Please help me with PREG_MATCH
    By Krakatioison in forum PHP Development
    Replies: 4
    Last Post: October 26th, 03:40 PM
  2. trouble with preg_match()
    By Ike in forum PHP Development
    Replies: 2
    Last Post: July 27th, 12:43 AM
  3. preg_match
    By Michael Temeschinko in forum PHP Development
    Replies: 8
    Last Post: October 10th, 05:04 PM
  4. preg_match bug
    By Christian Calloway in forum PHP Development
    Replies: 7
    Last Post: August 26th, 04:22 PM
  5. preg_match issue
    By Pascal S. in forum PHP Development
    Replies: 0
    Last Post: August 15th, 01:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139