PHP validation

Hi!

I am trying to create a script to validate phone number, GPA, street address,  using regex function.

___________ GPA validation ________________________________________
// Test GPA
      var regex = /^[0-9]+(\.[0-9]) +$/;
 
  if (!regex.test(form.gpa.value))
  {
    alert("GPA is not valid!");
    form.gpa.focus();
    return false;
  }

________________ Phone number validation _________________

// Test Phone Number
 var regex= /\d{3}\d{4}$/|\d{3}\d{3}\d{4}$/|\d{3}-\d{4}$/;


  if (!regex.test(form.phone.value))
  {
    alert("Phone number is not valid!");
    form.phone.focus();
    return false;
  }
____________________________address validation_________________________________

// Test address
  var regex = /^[a-zA-Z0-9_\-]+(\.[_a-zA-Z0-9\-]+$/;
  if (!regex.test(form.address.value))
  {
    alert("Address23 is not valid!");
    form.address.focus();
    return false;
  }
______________________________________________________________

The above validation works some time, but not always. The phone validation works only when a phone number is put in the format of 123-455-5678. It needs to validate 1234567890 or 455-5678 or 4555678.

The above GPA validation, for some reason, does not work at all. It should validate a number like 3 or 3.6 or 4.0.

My final question is: is there any function or way to filter and flag a warning when someone typed in (in a textbox in an online survey form) bad words like F-words.

Hope you experts may help me out.

Thanks!

duta

May 28, 2006, at 4:35 pm


dutaAsked:
Who is Participating?
 
BogoJokerCommented:
Phone number: (###-###-###, #########, ###-####, #######)
/^(\d{3}-?)?\d{3}-?\d{4}$/

GPA: (allows for 0.0 all the way up to 4.0 and 0,1,2,3,4)
/^(([0-3](.[0-9])?)|(4(.0)?))$/

-----
Your GPA did not work earlier because you have a space at the end, its looking or one or more spaces at the end.  Also yours would allow 999.999, mine limits only one number after the optional decimal point. (This can be changed)
0
 
dutaAuthor Commented:
TO: BogoJoker:

Thank you so much again for your very kind response.

I am writing this upon checking your comment.

Before posting my questions, I spent quite much time trying to figure out by myself.

I wonder whether you may kindly show me how to validate address like "123 Sunset Bl, Los Angles".

I also wonder whethere there is a way to send an error message if any bad words like F-word is put in a textbox.

Thanks a lot!

duta
May 29, 2006, at 12:45 am
0
 
dutaAuthor Commented:
TO: BogoJoker:

Hi again!

The GPA validation worked great, but for some reason, the phone validation keeps giving "Invalid phone number" message.

I just wonder why.

Thanks again!

duta

May 29, 2006, at 1:21 am

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
dutaAuthor Commented:
TO: BogoJoker:

Hi again!

Your phone validation script is working just fine after Internet Explorer was flushed. Sorry to cause you confusion.

By the way, I got a comment from someone regarding your script as the follows:
___________________ Comment from someone __________________________
Well,

The problem when you have a string in your url, and you output it directly, is that people can easily disrupt your site by adding strange phrases:
mysite.com/mypage.php?err=Look+at+me+I+am+changing+the+error+message+WOW+I+am+sooooooooooo+cool

This shouldn't be a real problem, but it isn't something you would like either.

Therefor you can use certain keywords that identify certain error messages:
ERR_NO_USERNAME turns to 'Sorry, you should fill in a username'.
(Eventually you can have a multilanguage site even, which still works because you only have to change the response text with certain error keywords, rather than your validation script which might generate an error.)

You then setup a list for yourself with possible errors, giving them semi-descriptive names:
ERR_NO_USERNAME,
ERR_NO_PASSWORD,
ERR_LOGIN_FAILED

And instead of the code below, you output err keywords:
elseif (!isset($_POST['year']))
  $err = 'form.php?err=Must+Provide+Your+Name;

Becomes:

elseif (!isset($_POST['year']))
  $err = 'form.php?err=ERR_NO_USERNAME;

Because your output always responds only to your own defined ERR_..... keywords, you have full control of what your output messages are.

_________________________________________________________________________________

I asked the someone to give me a little bit more explanation, but I haven't heard yet.

I just wonder whether you may kindly help me with that, if you may.

Thanbks a lot!

duta
May 29, 2006, at 1:43 am
0
 
BogoJokerCommented:
I did say that it was a simple form.  Its harmless to let users make their own error messages.  They may even have some fun doing it, thinking they are cool!  But, anyways inorder to cause no harm to anyone I guess we can make our own set of error messages.

There are a few ways you can do this, here are some simple ways:
1) Use numbers to represent error messages: 1 = No Name, 2 = No Email....
2) Use strings to represent error messages: NO_NAME = No Name, NO_EMAIL = No Email...

They are all up to you, the only changes you would need are in the process.php page, where you set $err like the code you have shown above:
  $err = 'form.php?err=ERR_NO_USERNAME;
Have that match up on the form.php page.

Example:
(in process.php)
  $err = 'form.php?err=ERR_NO_USERNAME;
(in form.php)
  if ($err == 'ERR_NO_USERNAME')
    print 'No Username Provided';
  elseif ($err == 'ERR_NO_EMAIL')
    print 'No Email Provided';

Joe P
0
 
dutaAuthor Commented:
TO: BogoJoker:

Thank you so much for your very kind, prompt respnee to my calls for help.

I am writing this message upon checking your very kind post.  I was out all afternoon and evening, enjoying Memorial Day break.

I am accepting your tip for PHP form validation (phone and GPA) which is working great. Thank so much for your other help.

God bless you!

duta

May 29, 2006 (Monday) at 11:37 pm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.