Jump to content

Validation


OriginalSunny

Recommended Posts

Hi, i am going through a book i purchased and this is some of the code in the book.
The code has not been explained properly in the book and i don't understand it fully.
I know that it is validating the values (i.e the first line i have shown below is for
validating the name of a person.) If someone could please explain what the validation is
doing (i.e what does !ereg mean? and is the first line simply checking to see if the value
is between letters a-z and less than 50 in length?). If someone could explain the lines
i would be greatfull.

if (!ereg("^[A-Za-z' -]{1,50}$",$value))

if(!ereg("^.+@.+\\..+$",$value))

if(!ereg("^[5]{1,1}[0-9]{15,15}$",$value))
Link to comment
Share on other sites

validating is checking to make sure it is right (useing the right characters)
if (!ereg("^[A-Za-z' -]{1,50}$",$value))

if(!ereg("^.+@.+\\..+$",$value))

if(!ereg("^[5]{1,1}[0-9]{15,15}$",$value))

and this is the code they are validating with so if there input doesn't match any of the characters listed the form will return false and they will get an error message something like your name contains invalid characters.

i dont know if that is what you are looking for but i hope that helps
Link to comment
Share on other sites

here's the basic idea of what they're doing:

[b]if (!ereg("^[A-Za-z' -]{1,50}$",$value))[/b]
-this line is simply saying that the $value must be between 1 and 50 characters and can only contain letters, apostrophes, spaces and hyphens

[b]if(!ereg("^.+@.+\\..+$",$value))[/b]
-this line looks like a very basic email validation. it's simply checking to make sure you have at least one character followed by an '@' symbol, then at least one more character followed by a period, and then finished off by at least one more character. this is a really sloppy email validator, though, because i could type in ".@..." and it would match. a much better one would be:
[code]
if (!preg_match("|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i", $value))
  // invalid email address
[/code]

and finally:
[b]if(!ereg("^[5]{1,1}[0-9]{15,15}$",$value))[/b]
-this is simply making sure that $value is a 16 digit number starting with a 5. seems to be a rudamentary credit card validation.

hope this helps

Link to comment
Share on other sites


what ereg does and this means ! not


To detect non-alphanumeric characters (for new username/password validation, for instance):
[code]
if(ereg('[^A-Za-z0-9]', $username)){
echo "Usernames must contain only letters and numbers.";
}else{
echo "$username is a valid username.";
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.