Please help me on this problem. Form validation

Discussion in 'PHP' started by Jeehan, Mar 26, 2011.

  1. #1
    Hey all, I have this form in my page. Trying to do this:

    only will accept user name as "testname" if user type something like "test name" then it will show error message. So no space in the name. How to do it?

    Please help.

    Thank you.
     
    Jeehan, Mar 26, 2011 IP
  2. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #2
    This will check if the username contains only A-Z (case-insensitive), 0-9, dash (-) or underscore (_)
    
    if(preg_match('{^[A-Za-z0-9_-]+$}', $username)) {
      // OK, username contains only A-Z, a-z, 0-9, _, -
    } else {
      // invalid, username contains invalid characters
    }
    
    PHP:
     
    Gray Fox, Mar 26, 2011 IP
  3. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    form.php
    <form action="action.php" method="post">
    <input type="text" size="30" name="user" />
    <input type="submit" value="submit" />
    </form>

    action.php
    <?php
    // catching user data
    $user = $_POST['user'];
    // Security..
    $user = mysql_escape_string($user);
    // checking data
    if ($user=="testuser" ){
    // blah blah..
    } else {
    echo "Wrong user";
    }
    ?>

    Hope this helps
     
    Kload, Mar 27, 2011 IP
  4. Jeehan

    Jeehan Well-Known Member

    Messages:
    1,578
    Likes Received:
    31
    Best Answers:
    1
    Trophy Points:
    115
    #4
    thank you everyone i will try these
     
    Jeehan, Mar 27, 2011 IP
  5. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #5
    You can use PHP class or JS library for this. Following links will help you.

    PHP classes: http://www.phpclasses.org/search.html?words=form+validator&x=0&y=0&go_search=1

    JS libraries (I could find only Jquery): http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/

    Yours sincerely.
     
    SametAras, Mar 29, 2011 IP
  6. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    just use javascript and an on submit validation function and youll be all set
     
    srisen2, Mar 31, 2011 IP