Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-20-2011, 07:55 PM   PM User | #1
Demon Templates
New Coder

 
Join Date: Mar 2007
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Demon Templates is an unknown quantity at this point
Form Validation help

HI all, I have a contact for that requires validating for a number of things. It validates Required for all parts of the form however fine.

My question is how can I ensure the client fills out the form fully. For instance, here in the UK we use 11 digits for phone numbers, but the form will validate if the clint enters just 1, so how can I ensure the customer actually enters all 11.

Here is the javascript I am using

function isFilled(str){ return (str != ""); }
function isEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); }
function isDigital(str) { return(parseFloat(str,10)==(str*1)); }
function isCurrency(val) { var re = /^(\$?\d+\$?|\$?\d+\.\d+\$?)$/; return (re.test(val)); }
function ValidForm(form) {
var field, i;
var req = new Array(10);
var email = new Array(1);
var digits = new Array(1);
var currs = new Array(0);
req[0] = "rw_First_Name";
req[1] = "rw_Last_Name";
req[2] = "re_Email";
req[3] = "rd_Mobile_Number";
req[4] = "r_Hair_Service";
req[5] = "r_Stylist";
req[6] = "r_Preferred_Day";
req[7] = "r_Preferred_Date";
req[8] = "r_Month";
req[9] = "r_Preferred_Time";
email[0] = "re_Email";
digits[0] = "rd_Mobile_Number";

for (i=0;i<10;i++) {
eval("field = form." + req[i]);
if (!isFilled(field.value)) {
alert("Field '" + field.title + "' is required to be filled in before successful submission.");
field.focus();
return false;
break;
}}
for (i=0;i<1;i++) {
eval("field = form." + email[i]);
if (!isEmail(field.value)) {
alert("Field '" + field.title + "' is required to be filled in with valid email addresses before successful submission.");
field.focus();
return false;
break;
}}
for (i=0;i<1;i++) {
eval("field = form." + digits[i]);
if (!isDigital(field.value)) {
alert("Field " + field.title + " is required to be filled in only with digits (0-9) and decimal point before successful submission.");
field.focus();
return false;
break;
}}
for (i=0;i<0;i++) {
eval("field = form." + currs[i]);
if (!isCurrency(field.value)) {
alert("Field " + field.title + " is required to be filled in only with digits (0-9) a decimal point, or a dollar sign before successful submission.");
field.focus();
return false;
break;
}}
return true; }
Demon Templates is offline   Reply With Quote
Old 12-21-2011, 09:05 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Form validation which simply checks for a blank field is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

To be candid, you would be best advised to ditch that elderly generic (and inefficient) validation script and write a proper one.

Not all UK telephone numbers are 11 digits. e.g. (01204) xxxxx Bolton

For phone validation see e.g. http://www.codingforums.com/showthread.php?t=156981



Quizmaster: The small primates called lemurs are native to which large island off the coast of Africa?
Contestant: Argentina.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-21-2011 at 09:22 AM..
Philip M is offline   Reply With Quote
Old 12-21-2011, 09:22 AM   PM User | #3
Demon Templates
New Coder

 
Join Date: Mar 2007
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Demon Templates is an unknown quantity at this point
Thanks Philip for your reply, but I do not see how your advice is meant to hellp in what I am asking?
Demon Templates is offline   Reply With Quote
Old 12-21-2011, 09:45 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Demon Templates View Post
Thanks Philip for your reply, but I do not see how your advice is meant to hellp in what I am asking?
You asked how to validate a telephone number and I pointed you to a thread in this forum. I also pointed out that form validation had been covered very many times in this forum. And I also pointed out that your script was generic, and did not allow for proper validation without very considerable modifications.

What further help do you want? You cannot reasonably expect people to provide you with an oven-ready solution when 9 different fields are involved.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-21-2011 at 09:48 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:56 PM.


Advertisement
Log in to turn off these ads.