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.
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:
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
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.