/    Sign up×
Community /Pin to ProfileBookmark

firefox does not apply my form validation script.

Hi developers,

I have a form where customers can register thierselves by entering the relevant fields.

There is a control() javascript which ckecks if everything on the form is ok before submitting it.
Althugh IE applies the script for every check, Firefox does only apply the first check (first if statement) of the script. The rest is ignored.

[U]Here is the javascript code:[/U]

function control()
{
if (document.member.FirstName.value == “”){
document.member.FirstName.focus();
alert(“Please fill the name field”);
return false;
}

if (document.member.FirstName.value.length<2) {

alert(“Name field cannot be shorter than 2 characters.”);
document.member.FirstName.focus();
return false;
}

if (/[d]/.test(document.getElementById(“FirstName”).value)) {
alert(“Entered numbers into the Name field”);
return false;
}

if (document.member.LastName.value == “”){
document.member.LastName.focus();
alert(“Please fill the Lastname field”);
return false;
}

if (document.member.LastName.value.length<2) {

alert(“Lastname field cannot be shorter than 2 charcters. Please check again.”);
document.member.LastName.focus();
return false;
}

if (/[d]/.test(document.getElementById(“LastName”).value)) {
alert(“Entered numbers into the Lastname field”);
return false;
}

if (document.member.Address1.value.length<10) {

alert(“Address field contains less than 2 characters.”);
document.member.Address1.focus();
return false;
}

if (document.member.Username.value == “”){
document.member.Username.focus();
alert(“Username field cannot be blank”);
return false;
}

if (document.member.Address1.value == “”){
document.member.Address1.focus();
alert(“Address field cannot be blank “);
return false;
}

if (document.member.City.value == “”){
document.member.City.focus();
alert(“City field cannot be blank”);
return false;
}

if (document.member.Email.value == “”){
document.member.Email.focus();
alert(“E-Mail field cannot be blank.”);
return false;
}

if (echeck(document.member.Email.value)==false){
document.member.Email.focus();
return false;
}

if (document.member.PhoneNumber.value == “”){
document.member.PhoneNumber.focus();
alert(“Phone number field cannot be blank”);
return false;
}

if (!/^d*$/.test(document.getElementById(“PhoneNumber”).value)) {
alert(“Entered characters into the Phone Number field”);
return false;
}

if (document.member.Pass.value == “”){
document.member.Pass.focus();
alert(“Password field cannot be blank.”);
return false;
}

if (document.member.Pass2.value == “”){
document.member.Pass2.focus();
alert(“Password confirmation field cannot be blank.”);
return false;
}

if (document.forms[0].Pass.value.length<6) {

alert(“Password field cannot contain less than 6 characters. Please check”);
document.member.Pass.focus();
return false;
}

if (document.member.Pass.value != document.member.Pass2.value) {

alert(“Password and password confirmation fields are not identical. Please check “);
document.member.Pass.focus();
return false;
}

if (!isTelNum(document.forms[0].PhoneNumber,’Telefon ‘)) return false;
if (!isNum(document.forms[0].InternalPhoneNumber,’Internal Phone number’)) return false;
//if (!isValidEmail(document.forms[0].Email)) return false;

return true;
}

Well interesting that only the first check is taken into consideration.

if (document.member.FirstName.value == “”){

The rest is not even seen. Even if you do not fill the rest of the form, the submit button tries to submit the form regardless what more needs to be checked.

Here is the submit button:

<input type=”submit” name=”member” value=”Register Me” id=”member” onclick=”return control()”;>

Any observations and comments are appreciated.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 25.2010 — Doesn't appear to be anything wrong with the function, although using 3 methods to reference elements is a little odd.

What is the HTML?
Copy linkTweet thisAlerts:
@foreigner737authorApr 25.2010 — In the Error Console of Firefox,

I have found the following Error:

[B]Error: document.getElementById("FirstName") is null[/B] Source File: http://localhost/member.asp?Email=&WrongCode=WrongCode&FirstName=dgh&LastName=&Username=&Address1=&Address2=&PostCode=&PhoneNumber=

Line: 45

Line 45 is the third "if" statement in the check() script:

if (/[d]/.test(document.getElementById("FirstName").value)) {

Regards.
Copy linkTweet thisAlerts:
@FangApr 25.2010 — There is a difference between [I][COLOR="Blue"]name[/COLOR] [/I]and [I][COLOR="Green"]id[/COLOR][/I].

you only need an id if the label element is referencing the form control.&lt;input type="text" [COLOR="Blue"]name="FirstName"[/COLOR] [COLOR="Green"]id="FirstName"[/COLOR]&gt;
Copy linkTweet thisAlerts:
@foreigner737authorApr 25.2010 — Hi Fang,

When this .asp page is post to a second page, the second (registration) page requests the values of the corresponing fields with this code:

[CODE]
Request.Form("Username")
[/CODE]


So will ID's be sufficient if I remove the input Names?
Copy linkTweet thisAlerts:
@FangApr 25.2010 — [I]name[/I] is compulsory, [I]id[/I] is optional.
Copy linkTweet thisAlerts:
@foreigner737authorApr 25.2010 — Hi all, after entering Id's for all the fields, the scripts worked fine.

Thanks for all comments.
×

Success!

Help @foreigner737 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...