d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Javascript Help > 130fg To Whoever Can Help Me Out Quick
Add Reply New Topic New Poll
Member
Posts: 5,253
Joined: Apr 4 2008
Gold: 1.70
Warn: 60%
Oct 16 2012 01:41pm
Reset executes perfectly.

My Password and username validation doesn't execute though.

I know the function is right.

Just in the body where I type for it to execute it fails, cause I know I'm doing something wrong there.

It's pretty simple for someone who knows javascript, quickfix = 130fg

<?xml version="1.0">
<!DOCTYPE html PUBLIC "-//stationeryinc//DTD XHTML 1.0 Strict//EN "http://www.stationeryinc.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title> Stationery Incorporated - Form </title>
<link rel="stylesheet" type="text/css" href="styles.css" />

<script>

function checkForm(form)
{
if(form.username.value == "")
{ alert("Error: Username cannot be blank!");
form.username.focus(); return false;
}
re = /^\w+$/;
if(!re.test(form.username.value))
{ alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;

}
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value)
{

if(form.pwd1.value.length < 6)
{
alert("Error: Password must contain at least six characters!");
form.pwd1.focus();
return false;
}
if(form.pwd1.value == form.username.value)
{
alert("Error: Password must be different from Username!");
form.pwd1.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form.pwd1.value))
{
alert("Error: password must contain at least one number (0-9)!");
form.pwd1.focus(); return false;
}
re = /[a-z]/;
if(!re.test(form.pwd1.value))
{
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.pwd1.focus();
return false;
}

re = /[A-Z]/;
if(!re.test(form.pwd1.value))
{
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.pwd1.focus();
return false;
}
}
else
{ alert("Error: Please check that you've entered and confirmed your password!");
form.pwd1.focus();
return false;
} alert("You entered a valid password: " + form.pwd1.value);
return true;
}
</script>


</head>
<body class="body">

<form id="form" action="demo_form.asp" name="form" onsubmit="return checkForm(form);" method="post">

<div class="form_add">First name: <input type="text" name="username"><br> </div>
<div class="form_add">Last name: <input type="text" name="lastname"><br> </div>
<div class="form_add">Email Address: <input type="text" name="email"><br> </div>
<div class="form_add">Password: <input type="password" name="pwd1"><br></div>
<div class="form_add">Confirm Password: <input type="password" name="pwd2"><br></div>
<div class="form_add">Please specify your gender<br></div>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female<br>
<div class="form_add">Have you made a purchase from our site before</div>
<input type="radio" name="purchase" value="male">Yes<br>
<input type="radio" name="purchase" value="female">No<br>
<div class="form_add">How satisfied are you <br></div>
<input type="checkbox" name="Satisfied with our service?" value="Yes">I am satisfied with your service<br>
<input type="checkbox" name="Satisfied with our service?" value="Yes">I am satisfied with your site<br>
<input type="checkbox" name="Satisfied with our service?" value="Yes">I am satisfied with your product<br>
<input type="checkbox" name="Satisfied with our service?" value="Yes">I am satisfied with pricing<br>
<div class="form_add">How many times have you visited our site</div>
<form action="">
<select name="type">
<option value="xx"> First time visitor</option>
<option value="xx"> Second time visitor</option>
<option value="xx"> Third time visitor </option>
<option value="xx"> Fourth time visitor </option>
</select>
</form>
<div class="form_add">If you have completed all queries please click the submit button below</div> <br>
<input type="submit" onclick="checkForm(form);" value="Submit"> <br>
</form>
</body>
</html>
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Oct 17 2012 01:59am
Replace this line:
<form id="form" action="demo_form.asp" name="form" onsubmit="return checkForm(form);" method="post">
with:
<form id="form" action="demo_form.asp" name="form" onsubmit="checkForm(form)">

The "return" thing there has no purpose I think, and the method="post" thing may be what's ruining things.

Additionally, you can probably also replace
<input type="submit" onclick="checkForm(form);" value="Submit"> <br>
with:
<input type="submit" value="Submit"> <br>

The button needs no "onclick" because your form already has an onsubmit.

Edit: You might also have to put form in the function call between apostrophes. Not sure about that though.

This post was edited by Nymphomaniac on Oct 17 2012 02:05am
Member
Posts: 3,064
Joined: Mar 30 2003
Gold: 5,095.00
Oct 22 2012 04:32pm
Quote (Nymphomaniac @ Oct 17 2012 02:59am)
Replace this line:
<form id="form" action="demo_form.asp" name="form" onsubmit="return checkForm(form);" method="post">
with:
<form id="form" action="demo_form.asp" name="form" onsubmit="checkForm(form)">

The "return" thing there has no purpose I think, and the method="post" thing may be what's ruining things.

Additionally, you can probably also replace
<input type="submit" onclick="checkForm(form);" value="Submit"> <br>
with:
<input type="submit" value="Submit"> <br>

The button needs no "onclick" because your form already has an onsubmit.

Edit: You might also have to put form in the function call between apostrophes. Not sure about that though.




The return on the "onsubmit" is required... it needs to pass the return of the function... so if the function returns false, it returns false to the onsubmit (aka don't submit the form)..
You are correct however... the onclick from the submit button needs to be removed, as it will actually run the validation twice.. this way..

your javascript looks great... the only thing i'd change... is that passing form between your checkForm functions is pointless considering it's a global anyway... so...

Code

<form id="form" action="demo_form.asp" name="form" onsubmit="return checkForm();" method="post">
function checkForm()
{

would work the same

THE ONLY problem lies in the HTML... you have TWO forms... delete the one in the middle of the code (not sure how it even got there... and you'll be fine.

Code
<form action="">
<select name="type">
<option value="xx"> First time visitor</option>
<option value="xx"> Second time visitor</option>
<option value="xx"> Third time visitor </option>
<option value="xx"> Fourth time visitor </option>
</select>
</form>

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll