PDA

View Full Version : Invalid Input message will not be removed



mw2005
Aug 28th, 2005, 08:47 PM
Hi all;
In my form code it will validate to see if the name and email address contains some information. But for example if i click submit without entering any information in the input fields only the name error appears and not the other one also if there was an error in the name and it was corrected it will not go. I cannot figure out what is the problem.

Thanks in Advance,
MW2005


<script type="text/javascript">

function validate(f) {

if(f.name.value == "" ) {
document.getElementById('first').innerHTML='Invalid Input';
return (false);}

if(f.email.value.indexOf( "@" ) == -1) {
document.getElementById('address').innerHTML='Invalid Input';
return (false);}

alert( "IT WORKS!" );
return (false);}
//change to return true and delete alert


</script>

<form method="post" name="myForm" onsubmit="return validate(this)">

<label for="name">First Name</label>
<input type="text" name = "name" value=""/><span id="first"></span>
<br />

<label for="email">Email Address:</label>
<input type="text" name="email" value="" onChange="javascript:this.value=this.value.toLowerCase();"/><span id="address"></span>
<br />

<label for="query">Message</label>
<textarea name="query"></textarea>
<br />

<input type="submit" value="Submit" id="submitbutton"/>
<input type="reset" value="Reset" id="Resetbutton">

</form>
</body>

</html>

diablo2_v
Aug 29th, 2005, 06:56 AM
Here are the changes:



<script type="text/javascript">

function validate(f)
{
def = true;

if(f.name.value == "" )
{
document.getElementById('first').innerHTML='Invalid Input';
def = false;
}
else
document.getElementById('first').innerHTML='';

if(f.email.value.indexOf( "@" ) == -1)
{
document.getElementById('address').innerHTML='Invalid Input';
def = false;
}
else
document.getElementById('address').innerHTML='';

if (def == true)
alert( "IT WORKS!" );

return def;
}
</script>


Just had to make it not return false if something does not validate. Instead change def to be false, so it goes through all the code, then returns false.

glenngv
Aug 29th, 2005, 07:19 AM
It only finds one error because you do a return after each validation.


function validate(f) {
var retVal = true;
document.getElementById('first').innerHTML="";
document.getElementById('address').innerHTML="";
if(f.name.value == "" ) {
retVal = false;
document.getElementById('first').innerHTML='Invalid Input';
}
if (f.email.value.indexOf( "@" ) == -1) {
retVal = false;
document.getElementById('address').innerHTML='Invalid Input';
}
return retVal;
}

mw2005
Aug 29th, 2005, 09:20 AM
Thanks for both of your help :thumbsup: