...

View Full Version : basic validating empty fields (baby steps)



trigger
06-12-2006, 08:46 AM
beginner starting off, with very basic validating an empty field, to show error message if left empty, on submit.
please help with basic newbie help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>2mail.html</title>

<script language="text/javascript" type="text/javascript">

function validateForm(){
var FirstName=document.MailForm.FirstName.value;

//check if it is empty
if(FirstName==""){
//display error msg
alert("Error: Please enter your first name");
//stop user to continue
return false;
}
//else
else{
//continue
return true;
}
}


</script>
</head>

<body>
<form onSubmit="return validateForm();"
action=""
name="MailForm"
enctype="text/plain">
Name:
<input type="text"
name="FirstName"
size="25"
maxlength="25"/>
<br>
<br>
<input type="submit"
value="SubmitNow"/>
<input type="reset"
value="ClearForm"/>


</form>
</body>
</html>

jskaar
06-12-2006, 11:19 AM
This is usually what I do:



<!--HTML:-->
<form onsubmit="return validateForm(this);" ......
<input name="field1" type="text" .....

//JAVASCRIPT:
function validateForm(form)
{
var error = "";
if (form.name == "")
{
error += "You have to enter a name";
}

if (error)
{
alert(error);
return false;
}
// Everything is ok! Send the form
return true;
}


And just use <script type="text/javascript">. You don't need that language stuff...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum