Link to home
Start Free TrialLog in
Avatar of duta
duta

asked on

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


ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duta
duta

ASKER

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
Avatar of duta

ASKER

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

Avatar of duta

ASKER

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
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
Avatar of duta

ASKER

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