Jump to content

Why Does Error Message Blinks On Submit


vinpkl

Recommended Posts

hi all

 

why does my validation error messages blink of click

 

they dont stay static.

 

whats the solution

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<script type="text/javascript">
function doValidate() {
if(document.registerform.textbox1.value= " ")
{
 document.getElementById("hiddenDiv").innerHTML = "enter your email id its required";
}
if(document.registerform.textbox2.value= " ")
{
 document.getElementById("hiddenDiv2").innerHTML = "enter your password id its required";
}
}
</script>
<body>
<form onsubmit="return doValidate();" method="post" name="registerform">
<input name="textbox1" id="textbox1" /><div id="hiddenDiv"></div>
<input name="textbox1" id="textbox2" /><div id="hiddenDiv2"></div>
<input type="submit" name="" value="Submit"/>
</form>
</body>
</html>

 

vineet

Link to comment
Share on other sites

The problem is you're not cancelling the form submission so the page reloads. What happens is your JS function runs and populates the error div's with the messages. A split second later the form submits and the browser starts reloading the page. When the page reloads everything is reset so the messages disappear.

 

You need to return false in the onsubmit handler to cancel the form submit. You already added the 'return ' key word there so it will return whatever value your doValidate function returns. You never setup a return value for that function though. You need to use a variable to track if there are errors and return true or false depending on that.

 

function doValidate() {
 var hasErrors=false;
 if(document.registerform.textbox1.value= " ")
 {
        document.getElementById("hiddenDiv").innerHTML = "enter your email id its required";
        hasErrors=true;
 }
 if(document.registerform.textbox2.value= " ")
 {
        document.getElementById("hiddenDiv2").innerHTML = "enter your password id its required";
        hasErrors=true;
 }

 return !hasErrors;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.