Jump to content

Validate String (Regular Expression)


Ashish Sood

Recommended Posts

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 by Ashish Sood
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...