Give something like this a try:
<script type="text/javascript">
// regular expression to detect one or more spaces
var blankRE=/^[\s]+$/;
// 1-16 letters only
var textRE=/^[a-zA-Z]{1,16}$/;
function ValidateForm(FormObject)
{
// pass form object reference to function
// 'this' or document.forms['myform']
// check to see if required field 'textfield 1' has been completed,check
for empty field and spaces entered into field
if(FormObject.elements['textfield1'].value=="" ||
blankRE.test(FormObject.elements['textfield1'].value))
{
alert(' You have not completed a required field "textfield 1".
Please complete it now.');
FormObject.elements['textfield1'].focus();
return false;
}
// check format of string in textfield 1, allows only 16 upper/lower
case letters
if(!textRE.test(FormObject.elements['textfield1'].value))
{
alert('The contents of "textfield 1" are invalid. Only 16 letters
are allowed.');
FormObject.elements['textfield1'].focus();
return false;
}
return true;
}
</script>
<form name="myform" method="post" action="reqform.php" onsubmit="return
ValidateForm(this);">
<label for="textfield1">textfield 1</label>
<input type="text" name="textfield1" id="textfield1" size="16">
<input type="submit" value="submit form">
</form>
MT said:
Hi all, this sounds like an easy enough thing to do, but after
spending 45 minutes searching google and various javascript sites I
can't find out how to make a textfield (textbox or whatever you want
to call it) in an html form be a mandatory field. I'm guessing it
would be on the "submit" click, and a pop-up would appear saying to
enter data in teh field. Can anyone recommend a good javascript site?
thanks in advance.