[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

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


0
duta
Asked:
duta
  • 4
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now