Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this An bug?

Status
Not open for further replies.

Vallabh

Technical User
Feb 24, 2006
3
US
Hi,

I am kindaa new to javaScripts. I have written the Following java Serer page

<html>
<head>
<style type="text/css">
div
{
position:absolute;
left:440px;
top:350px;
}
</style>
<script type="text/javascript" >
function validate()
{
var id = document.userpass.id.value;
alert (document.userpass.id.value);
alert(id);
if((id == null)) then
{
alert("User id cannto be null");
document.userpass.id.focus;
return false;
}
return true;
}
</script>
</head>

<body >

<form name="userpass" action="validate" method="POST" onSubmit="return validate()" >
<div>USER ID :<input type="text" name="id" ><br>
Password :<input type="password" name="password"><br>
<input type="submit" value="Login" ></div>
</form>

<body>
</html>
The problem is whenver i enter the USER ID . it is displayed in the Alert Window. However it goes inside the IF looop and the form is not submitted. IE it exactly performs the opposite of what it is supposed to do.. please help me out.

 
There are many things wrong with the script:

- "then" is not a JavaScript keyword. This will cause your code to error.

- You should not use "id" as a form field name, as it may cause some confusion over any possible form id.

- Testing for null is incorrect - you should be testing against an empty string.

- "focus" is a method, and should have brackets after it - "focus()"

Try this as a replacement function:

Code:
function validate() {
	var frm = document.forms['userpass'].elements;
	var userid =  frm['userid'];
	if (userid.value == '') {
		alert('User id cannot be empty');
		userid.focus();
		return (false);
	}
	return (true);
}

Don't forget to change the name of your form field from "id" to "userid".

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi bill,

Thanks a lot for your reply..

But if i am suppose to access the text value in a single step like this .

var userid = document.userpass.userid.value;
if(userid == '')
{
/* do validations and return false;*/
}
return true;

This doesnt work why ? whats wrong with this?
 
[1] Main reason.
>[tt] if((id == null)) then [/tt]
should be
[tt] if((id == null)) [/tt]
Once error encounter, it will surrender control to caller and submit will go ahead.

[2] Minor things: unrelated to the error.
[2.1]
>[tt]document.userpass.id.focus;[/tt]
[tt]document.userpass.id.focus[red]()[/red];[/tt]
[2.2]
[tt]<body>
<!--- etc -->
<[red]/[/red]body>
[/tt]

 
Correction:
Please ignore this statement---not thinking when I was typing.
Once error encounter, it will surrender control to caller and submit will go ahead.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top