Ashish Sood Posted November 3, 2013 Share Posted November 3, 2013 (edited) HI, I working on the form where i want to restrict the user to input only characters,digit, . , and white space. i want to validate 2 text field and one text area. if(!preg_match("/^[a-zA-Z0-9].*$/",$field1)) { $turnmsg = "<b><font color='red'> Only letters,spaces & . are allowed </font></b>"; } if(!preg_match("/^[a-z A-Z].*$/",$field2)) { $turnmsg = "<b><font color='red'> Only letters,spaces & . are allowed </font></b>"; } if(!preg_match("/^[a-z A-Z].*$/",$textarea1)) { $turnmsg = "<b><font color='red'> Only letters,spaces & . are allowed </font></b>"; } The problem is that when i applied preg_match on single field with if condition its working fine , but with the mutiple if condition its fail. any alternatives or do i needs any modificaiton into my code. Thanks , Ashish Edited November 3, 2013 by Ashish Sood Link to comment Share on other sites More sharing options...
justsomeguy Posted November 4, 2013 Share Posted November 4, 2013 How does it fail? The code looks fine in general, but I'm not sure you're using the correct patterns. The error messages are all the same, but you're using 2 different patterns, you're not checking for the same thing every time. Also, the "." character in a regular expression is a meta-character that matches anything except a newline. So the first pattern says that the string needs to start with a letter or number, and then it can contain any other character except a newline. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now