Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to build a registration Html form with input label boxes and use an external javascript file to validate each field. However when I

I'm trying to build a registration Html form with input label boxes and use an external javascript file to validate each field. However when I click my submit button my external javascript doesn't fire? I will post html and javascript code below. Please tell me what I need to do to get the below code to work as intended. Thanks!

HTML source code:

Registration















Male Female

Single Married
Widowed

JAVASCRIPT source code:

function validateForm() { var a=document.forms["myform"]["Uname"].value; if(a.length > 50) { alert("Username can't be more than 50 characters.") document.getElementById("Uname").focus(); document.getElementById('Uname').style.backgroundColor = "red"; return false; } else if(a.length < 6) { alert("Username can't be less than 6 characters.") document.getElementById("Uname").focus(); document.getElementById('Uname').style.backgroundColor = "red"; return false; } var b=document.forms["myform"]["Password"].value; if(b.length > 50) { alert("Password Can't be more than 50 characters."); document.getElementById("Password").focus(); document.getElementById('Password').style.backgroundColor = "red"; return false; } else if(b.length < 8) { alert("Password must be at least 8 characters long.") document.getElementById("Password").focus(); document.getElementById('Password').style.backgroundColor = "red"; return false; } var c=document.forms["myform"]["RpPassword"].value; if(c.length > 50) { alert("Repeat password can't be more than 50 characters long"); document.getElementById("RpPassword").focus(); document.getElementById('RpPassword').style.backgroundColor = "red"; return false; } else if(c.length < 8) { alert("Repeat Password must be atleast 8 characters long.") document.getElementById("RpPassword").focus(); document.getElementById('RpPassword').style.backgroundColor = "red"; return false; } var d=document.forms["myform"]["Fname"].value; if(d.length > 50) { alert("First name can't be more than 50 characters."); document.getElementById("Fname").focus(); document.getElementById('Fname').style.backgroundColor = "red"; return false; } else if(d.length == "") { alert("First name can't be blank.") document.getElementById("Fname").focus(); document.getElementById('Fname').style.backgroundColor = "red"; return false; } var f=document.forms["myform"]["Email"].value; if(f.length > 255) { alert("Email can't be more than 255 charachers.") document.getElementById("Email").focus(); document.getElementById('Email').style.backgroundColor = "red"; return false; } else if(f.length == " ") { alert("Email can't be left blank.") document.getElementById("Email").focus(); document.getElementById('Email').style.backgroundColor = "red"; return false; } var e=document.forms["myform"]["Lname"].value; if(e.length > 50) { alert("Last name can't be more than 50 characters."); document.getElementById("Lname").focus(); document.getElementById('Lname').style.backgroundColor = "red"; return false; else if(e.length == " ") { alert("Last name can 't be left blank") document.getElementById("Lname").focus(); document.getElementById('Lname').style.backgroundColor = "red"; return false; } } var h=document.forms["myform"]["A2"].value; if( h.length > 100) { alert("Address 2 can't be more than 100 character.") document.getElementById("A2").focus(); document.getElementById('A2').style.backgroundColor = "red"; return false; else if(h.length == " ") { alert("Address 2 can't be left blank. ") document.getElementById("A2").focus(); document.getElementById('A2').style.backgroundColor = "red"; return false; } } var i=document.forms["myform"]["State"].value; if( i.length > 50) { alert("State can't be more than 50 character.") document.getElementById("State").focus(); document.getElementById('State').style.backgroundColor = "red"; return false; else if(i.length == " ") { alert("State can't be left blank. ") document.getElementById("State").focus(); document.getElementById('State').style.backgroundColor = "red"; return false; } } var j=document.forms["myform"]["City"].value; if( j.length > 50) { alert("City can't be more than 50 characters.") document.getElementById("City").focus(); document.getElementById('City').style.backgroundColor = "red"; return false; else if(j.length == " ") { alert("City can't be left blank. ") document.getElementById("City").focus(); document.getElementById('City').style.backgroundColor = "red"; return false; } } var k=document.forms["myform"]["Zcode"].value; if( k.length > 10 ) { alert("Zip code can't be more than 10 characters.") document.getElementById("Zcode").focus(); document.getElementById('Zcode').style.backgroundColor = "red"; return false; else if(k.length == " ") { alert("Zip code can't be left blank. ") document.getElementById("Zcode").focus(); document.getElementById('Zcode').style.backgroundColor = "red"; return false; } } var l=document.forms["myform"]["P#"].value; if( l.length > 12) { alert("Phone number can't be more than 12 characters and has to be in the xxx-xxx-xxxx format.") document.getElementById("P#").focus(); document.getElementById('P#').style.backgroundColor = "red"; return false; else if(h.length == " ") { alert("Phone number can't be left blank. ") document.getElementById("P#").focus(); document.getElementById('P#').style.backgroundColor = "red"; return false; } } var m=doument.forms["myform"]["Gender"].value; if( m.length > 50) { alert("Gender can't be more than 50 characters.") document.getElementById("Gender").focus(); document.getElementById('Gender').style.backgroundColor = "red"; return false; else if(m.length == " ") { alert("Gender can't be left blank. ") document.getElementById("Gender").focus(); document.getElementById('Gender').style.backgroundColor = "red"; return false; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

If striving to meet schedule or budget isnt top priority, what is?

Answered: 1 week ago

Question

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago