View Full Version : javascript coding for all errors in a single alert box,not understanding error ?

11-01-2012, 02:06 PM
function isFormValid()
var userF = document.getElementById('FName').value;
var userL = document.getElementById('LName').value;
var userPW = document.getElementById('pw').value;
var userPW2 = document.getElementById('pw2').value;
var userEmail = document.getElementById('email').value;
var userMobile=document.getElementById('mobile').value;
var userAddress=document.getElementById('address1').value;
var userCity=document.getElementById('city').value;
var userState=document.getElementById('state').value;
var userZip=document.getElementById('zip').value;
var gen=document.form.radios;
var NameFormat = /^[A-Za-z]{2,12}$/;
var PWFormat = /^[A-Za-z0-9{6,12}$]/;
var Mobileformat=/^[0-9]{10}$/;
var Emailformat=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var retVal = true;
var errorMsg = "";

if (!NameFormat.test(userF))
errorMsg = "First Name is required and should only have letters. 2-12 letters max\n";
retVal = false;
if (!NameFormat.test(userL))
errorMsg = errorMsg + "Last Name is required and should only have letters. 2-12 letters max\n";
retVal = false;

if ( (gen[0].checked == false ) && ( gen[1].checked == false ) ){
errorMsg=errorMsg+"please check your gender: Female or Male\n";

return retVal =false;

errorMsg = errorMsg+"Mobile number is required and should only have 10 numbers \n";
retVal = false;

errorMsg = errorMsg+"please eneter a valid email id \n";
retVal = false;
if (!PWFormat.test(userPW))
errorMsg = errorMsg + "Password is required and should only have letters and numbers. 6-12 letters max\n";
retVal = false;
if (userPW != userPW2)
errorMsg = errorMsg + "Passwords do not match.\n";
retVal = false;
errorMsg = errorMsg + "please enter valid address \n";
retVal = false;

errorMsg=errorMsg+"please enter your city\n";

return retVal =false;
errorMsg=errorMsg+"please enter your state\n";
return retVal =false;
if( zip.value == "" || isNaN(zip.value ) ||zip.value.length != 6 ){
errorMsg=errorMsg+"Please provide a zip in the format ######.\n";

return retVal =false;
if (!retVal)
alert( errorMsg);
return retVal;

<form action="mailto:hummdedum@felloff.com" method="post" name="form" onSubmit="return isFormValid();">

* First Name: <input type="text" name="FName" id="FName" onblur="checkFName();"/><label id="labelFName"></label><br />

* Last Name: <input type="text" name="LName" id="LName" onblur="checkLName();"/><label id="labelLName"></label><br />

* Gender<input type="radio" name="radios" value="male" id="gen" onblur="checkgen();">Male <input type="radio" name="radios" value="Female">Female<label id="labelGender"></label><br />

* E-Mail: <input type="text" id="email" name="Email" onblur="checkEmail();"/><label id="labelEmail"></label><br />

* Mobile Number<input type="text" name="mobilenumber" id="mobile" onblur="checkmobile();"><label id="labelMobile"></label><br />

* Password: <input type="password" id="pw" name="Password" onblur="checkpw();"/><label id="labelpw"></label><br />

*Re-type Password: <input type="password" name="2Password" id="pw2" onblur="checkpw2();" /><label id="labelpw2"></label><br />

* Address1<input type="text" name="address" id="addressid1" onblur="checkaddress1();"><label id="labelAddress1"></label><br />

Address2<input type="text" name="add2"><label id="labelAddress2"></label><br />

Address3<input type="text" name="add3"><label id="labelAddress3"></label><br />

*City<input type="text" name="cityname" id="city" onblur="checkcity();"><label id="labelCity"></label><br />

*State<input type="text" name="statename" id="state" onblur="checkstate();"><label id="labelState"></label><br />

*ZIP<input type="text" name="zip1" id="zip" onblur="checkzip();"><label id="labelZip"></label><br />

<input type="submit" value="Submit" />
<input type="reset" value="reset" />


11-01-2012, 04:20 PM
If you post any code please put it in between
tags. It makes scanning your posts much easier. You can do this by clicking the small ‘#’ icon above the reply field.

11-01-2012, 04:24 PM
Well, at a cursory glance, don't name your form "form". Don't use reserved words as IDs or Names.

Other than that, please be more descriptive. What are you not understanding?

Philip M
11-01-2012, 05:38 PM
<form action="mailto:hummdedum@felloff.com"

The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use
and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express or Windows Live Mail as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Other users will not wish to reveal their email address. Also, what about people with Javascript disabled?

In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are several good free ones out there.

For a simple PHP form feedback script see:- http://www.thesitewizard.com/archive/feedbackphp.shtml
or http://www.felgall.com/php2.htm

Use your error console to expose errors.

var userAddress=document.getElementById('address1').value; // no such field - id is addressid1

Because you have defined var retVal = true; any mistake of this kind will cause the script to stop executing and return true.

if (!NameFormat.test(userF)) // no function NameFormat

If at first you don't succeed, redefine success.

Logic Ali
11-01-2012, 06:08 PM
The first error is

TypeError: document.getElementById("address1") is null

Line: 12

Referring to this line:

var userAddress=document.getElementById('address1').value;

See if you can find the form element to which it refers.