...

View Full Version : Script Works only some times



krishnan_sn
01-12-2004, 08:15 AM
hi,

i have written some javascript code to validate the form. but the script code executes some times and prompts for the user to enter the values. some times it skips it. i donot know how to solve this problem. whether i have to change any property in the explorer. iam using IE explorer. i am not sure about the versionof it. or should i change the code.

i have included the code below.

<SCRIPT LANGUAGE=javascript>
<!--
function ValidateId()
{
if(document.frmCreateLogin.txtLogin.value=="")
{
alert("Enter Login ID");
document.frmCreateLogin.txtLogin.focus();
document.frmCreateLogin.txtLogin.select();
return false;
}
else
{
if(!isNaN(document.frmCreateLogin.txtLogin.value))
{
alert("Invalid User ID");
document.frmCreateLogin.txtLogin.select();
return false;
}
}
if(document.frmCreateLogin.txtPass1.value=="")
{
alert("Enter First Password");
document.frmCreateLogin.txtPass1.focus();
return false;
}
if(document.frmCreateLogin.txtpass2.value=="")
{
alert("Enter Confirm Password");
document.frmCreateLogin.txtpass2.focus();
return false;
}
if(document.frmCreateLogin.txtPass1.value != document.frmCreateLogin.txtpass2.value)
{
alert("Both passwords should be same");
document.frmCreateLogin.txtPass1.focus()
document.frmCreateLogin.txtPass1.select()
return false;
}
return true;
}
//-->
</SCRIPT>

glenngv
01-12-2004, 08:32 AM
Are you calling the function like this?
<form onsubmit="return ValidateId()">
...
<input type="submit" name="btnSubmit" value="Submit">
</form>

To make your code efficient, you should pass the form reference to the function.
<form onsubmit="return ValidateId(this)">

the this keyword refers to the current element which is the form.

then in your function:
function ValidateId(f)
{
if(f.txtLogin.value=="")
...

That will save you a lot of typing and more importantly a lot of repetitive referencing.

krishnan_sn
01-12-2004, 10:10 AM
dear glenngv,

i have modified my code as u have said. but still it is not executing this code. what should i do to execute this code compulsorily...some times this validation code is executed and some times not...what is the reason behind it...

thanks

glenngv
01-12-2004, 10:17 AM
Please post the whole code

krishnan_sn
01-13-2004, 07:23 AM
hi,

i have posted my whole code here...what is the mistake in the script code...why it is not executing....

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>

<SCRIPT LANGUAGE=javascript>
<!--
function Validate(f)
{
if(f.txtoldpass.value=="")
{
Alert("Enter the Old Password");
f.txtOldPass.focus();
return false;
}
if(f.txtpass1.value=="")
{
alert("Enter the New Password");
f.txtpass1.focus();
return false;
}
if(f.txtpass2.value=="")
{
alert("Enter the Confirm Password");
f.txtpass2.focus();
return false;
}
return true;
}
//-->
</SCRIPT>

<FORM Name="frmChangePassword" Method="post" Action="CheckChangePassword.asp" OnSubmit="return validate(this)">
<pre>
<P><FONT size=5>&nbsp;<STRONG>
Change Password</STRONG></FONT></P>
<STRONG>
Old Password </STRONG>
<INPUT name=txtOldPass type=password>
<P>
<STRONG>New Password </STRONG>
<INPUT name=txtPass1 type=password > </P>
<P><STRONG>Confirm Password </STRONG>
<INPUT name=txtpass2 type=password></P>
<P>&nbsp;</P>
<P>&nbsp;
<INPUT name=submit type=submit value=Submit onclick="validate()"></P>
<P></P>
</pre>
</FORM>
</BODY></HTML>

glenngv
01-13-2004, 07:42 AM
Javascript is case-sensitive.
Validate() is different with validate()
Check all the cases including the use of element names.
If you name your field as txtOldPass, access it in javascript with the same case.
Also remove the onclick handler for the submit button.
The onsubmit handler in the form already does the validation.
Also turn the javascript error notification for easy debugging.

krishnan_sn
01-14-2004, 05:28 AM
dear glenngv,


thanks for ur valuable reply. i have modified my code as per ur suggestions and now it is working fine.


Also turn the javascript error notification for easy debugging
i could not understand the above statement said by u.. what u r trying to say..should i modify it in the IE. if so what property should i change. please be specific. i am new to the web site development.

thanks & regards

glenngv
01-14-2004, 05:57 AM
In the menu:

Tools / Internet Options / Advanced / Display a notification after every script error

If you check that option, any javascript error will be prompted to the user. That will help you in debugging scripts.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum