...

Checking Validations

sonibhavinh
06-21-2002, 12:32 PM
I have made an login form and in that i m using JavaScript for checking validations. But the coding which i have done is just checking the validations for the first field ie User name & it is not checking whether the password field is left blank or not or it is not even checking other validations also.
the coding goes here :-

<html>
<head>
<title>The form</title>
<SCRIPT LANGUAGE=JavaScript>
<!--
function validate()
{
if ( isName() && isPass())
{
document.login.submit();
}
return true;
}
function isName()
{
var str = document.login.firstname.value;
if (str == "")
{
alert("\nThe Login Name field is blank .\n\nPlease re-enter your Login Name.")
document.login.firstname.focus();
return false;
}
}
function isPass()
{
var str = document.login.passwd.value;
if (str == "")
{
alert("\nThe PASSWORD field is empty")
document.login.passwd.focus();
return false;
}
if (str.length < 4)
{
alert("The PASSWORD field has less than 4 chars.\n Please re-enter your Password.")
document.login.passwd.focus();
return false;
}

}

//-->
</SCRIPT>
</head>
<BODY bgcolor="#5e9eff" background="C:\image001.jpg"><p>
<center><br><br><br><br><br><br>
<h2>Welcome to the Login Form</h2><br>
<form name="login">
<table border=14 bgcolor="#CCCCCC" >
<tr><th>
&nbsp;&nbsp;&nbsp;&nbsp;
<font color="red">
Enter LoginName :
</th>
<td>
<input type="text" maxLength=20 value="" name="firstname">
</td></tr>
<tr><th>
&nbsp;&nbsp;
<font color="red">
Enter Password :</th>
<td>
<input type="password" value="" name="passwd" maxLength=12></td>
</tr>
<tr>
<td colspan=2>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" value="Submit" name="submit" onClick="validate()">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Reset" name="reset"></td></tr>
<tr>
<th colspan=2>
<a href="email.html"><font size=2><face="times"><i>New User For Registration click here
</font></a></th></tr>
</form>
</body>
</html>

meems
07-19-2002, 08:09 PM
just add :

<input type="button" value="Submit" name="submit" onClick="validate() && isPass()">

tamienne
07-19-2002, 08:27 PM
Try this..

<html>
<head>
<title>The form</title>
<SCRIPT LANGUAGE=JavaScript>
<!--
function validate() {
if ( isName() && isPass()) {
return true;
}
return false;
}
function isName() {
var str = document.login.firstname.value;
if (str == "") {
alert("\nThe Login Name field is blank .\n\nPlease re-enter your Login Name.")
document.login.firstname.focus();
return false;
}
return true;
}

function isPass() {
var str = document.login.passwd.value;
if (str == "") {
alert("\nThe PASSWORD field is empty")
document.login.passwd.focus();
return false;
}
if (str.length < 4) {
alert("The PASSWORD field has less than 4 chars.\n Please re-enter your Password.")
document.login.passwd.focus();
return false;
}
return true;
}

//-->
</SCRIPT>
</head>
<BODY bgcolor="#5e9eff"><p>
<center><br><br><br><br><br><br>
<h2>Welcome to the Login Form</h2><br>
<form name="login" onSubmit="validate()">
<table border=14 bgcolor="#CCCCCC" >
<tr><th>

<font color="red">
Enter LoginName :
</th>
<td>
<input type="text" maxLength=20 value="" name="firstname">
</td></tr>
<tr><th>

<font color="red">
Enter Password :</th>
<td>
<input type="password" value="" name="passwd" maxLength=12></td>
</tr>
<tr>
<td colspan=2>

<input type="submit" value="Submit" name="submit" >

<input type="reset" value="Reset" name="reset"></td></tr>
<tr>
<th colspan=2>
<a href="email.html"><font size=2><face="times"><i>New User For Registration click here
</font></a></th></tr>
</form>
</body>
</html>

====================
i put the validation onSubmit since they could possibly submit it with an 'enter' key. Also, you might want to change your background image. Not everyone has C:\image001.jpg on their systems :) The main thing is that you need to make sure each function returns something. I added a few return trues.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum