/    Sign up×
Community /Pin to ProfileBookmark

javascript validation problem

Hey guys i have the following html code and im trying to check if the “first name” text field has input input in it before submitting the form. So its validating whether the user has typed something in. I have the javascript below this and i cannot get it to work.

[code]
<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html>
<head>
<script src=”validnewsletter.js” type=”text/javascript”>
</script>

<title>Register</title>

<meta http-equiv=”content-type” content=”text/css; charset=ISO-8859-1″/>
<link rel=”stylesheet” type=”text/css”
href=”content.css” />

</head>

<body>
<div id = “header”><h1>Underground Music</h1></div>

</td></tr>

<tr><td align=”left” valign=”top”>

<div id =”left”>
<a href=”home.html”><b>Home</b></a>
<p></p>
<a href=”register.html”><b>Register</b></a>
<p></p>
<a href=”ProductHome.html”><b>Product</b></a>
<p></p>
<a href=”contact.html”><b>Contact</b></a>
<p></p>
<a href=”about.html”><b>About</b></a>
<p></p>
<a href=”home.html”><b>Image Map</b></a>
<p></p>

<hr></hr>
<p>
<a href=”purchase.html”><b>Checkout</b></a>
</p>
<a href=”mailto:[email protected]”><b>Email Us</b></a>
<p></p>
<hr></hr>
<p></p>
<a href=”http://www.fender.com/”><b>Fender.Com</b></a>
</div>

</td><td align=”center” valign=”top”>
<div id = “main”>
Register to recieve the weekly newsletter

<form name = “myform” action =”purchase.html” onsubmit=”return (checkEmail(this) && ValidateAge(this) && notEmpty(this) && formValidation(this) && checkform(this);)”>
<p></p>
<table border=”1″>

<tr> <td align=”left”>Name:</td> <td align=”left”><input type=”text”
size=”20″ name=”first” />(fname)<br></br></td></tr>
<tr> <td align=”left”>Surname:</td> <td align=”left”><input type=”text” size=”20″ name =”surname” id=”surname” />(Surname)</td></tr>
<tr> <td align=”left”>Age:</td> <td align=”left”><input type=”text” size=”20″ name=”age” id = “age” onblur =”ValidateAge(this);” />(age)<br></br></td></tr>
<tr> <td align=”left”>Email:</td> <td align=”left”><input type=”text” size=”20″ name =”emailAddr” />(email)<br></br></td></tr>
<tr> <td align=”left”>Id:</td> <td align=”left”>
<input type = “text” name = “id” size=”20″/>(ID)</td></tr>

<tr> <td align=”left”>How did you find Underground music</td><td align=”left”><select name=”how did u find us!” id=”findingus”>
<option value=”radio”>radio</option>
<option value=”friend”>Friend</option>
<option value=”internet”>Newspaper</option>
<option value=”newspaper”>Internet</option>
<option value=”telivision”>Tv</option>
</select><br></br>
</td></tr>

<tr><td align=”left”>text or html format</td><td align=”left”><input type=”radio” name=”format” value=”text”/>text
<input type=”radio” name=”format” value=”html” />html<br></br></td></tr>
<tr><td align=”left”>Submit</td><td align=”left”><input type=”submit”/></td></tr>

</table>

</form>

</td>
</tr>
<tr><td align=”center” colspan=”2″>

Copyright © 2000, Underground Music, All Rights Reserved.

</td></tr>
</table>

<p>
<a href=”http://validator.w3.org/check?uri=referer”><img
src=”http://www.w3.org/Icons/valid-xhtml10″
alt=”Valid XHTML 1.0 Strict” height=”31″ width=”88″ /></a>
</p>

</div>
</body>
</html>

[/code]

[code]
function formValidation(form)
{
if(notEmpty(form.surname))
{
if(notEmpty(form.age))
{
if(notEmpty(form.emailAddr))
{
if(notEmpty(form.id))
{
if(notEmpty(form.first))
{

return true;
}

}

}

}

}

return false;
}

function notEmpty(elem)
{
var str = elem.value;

if(str.length == 0)
{
alert(“*****Please type your details in every field!***** “);
return false;
}
else
{
return true;
}
}

function ValidateAge(ageNo)
{

if (ageNo.value <10)
{

alert(“*****Please enter a value greater then ten but less then 90***** “);
}
else if(ageNo.value >90)
{
alert(“*****Please enter a value greater then ten but less then 90***** “);
}

}

function checkEmail(myForm)
{
if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(myForm.emailAddr.value))
{
return (true)
}
alert(“Invalid E-mail Address! Please re-enter.”)
return (false)
}

function checkform ( form )
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website

// ** START **
if (form.first.value == “”) {
alert( “Please enter your first name.” );
form.first.focus();
return false ;
}
// ** END **
return true ;
}

[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @bazzano 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...