I think the regular expression test will treat an empty field the sama aws
an invalid field and reject it.
If you want the field to be allowed if null, byt check the syntax if it is
not null, try:
var FieldObject=FormObject.elements['fieldname']
// check to see if form field is empty, if *not* check syntax of form field
if(FieldObject.value!='')
{
if(!/^^[0-9a-zA-Z\s\,\/\-\.]+$?.test(FieldObject.value)
{
// invalid text in field
alert(str_error_01a + 'fieldname' + str_error_01b);
FieldObject.focus();
return false;
}
}
"Cooper" <co****@blueware.it> wrote in message
news:Wl********************@news2.tin.it...
"Richard Hockey" <ri***********@dsl.pipex.com> ha scritto nel messaggio
news:3f*********************@news.dial.pipex.com.. . I think you are looking for the javascript focus() method, which sets
the currently active form element.
<script type="text/javascript>
function Validate()
{
var FormObject=document.forms['myform'];
var T1Object=FormObject.elements['text1'];
var T2Object=FormObject.elements['text2'];
if(T1Object.value=='')
{
// display alert
alert('You have not entered any text in field 1. Please enter some
now.'); // set focus on invalid field
T1Object.focus();
// go back to from WITHOUT submitting
return false;
}
if(T2Object.value=='')
{
alert('You have not entered any text in field 1. Please enter some
now.'); T2Object.focus();
return false;
}
// all fields validated, submit form
return true;
}
</script>
<form name="myform" action=blah.php" method ="post" onSubmit="return
Validate();">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit" value="Submit form">
</form>
Ok thanks you, but i have a problem now, if in a text i want to accept
also '' okay, i remove (es: T1Object.value=='' ) but in this case, if i have:
...........
campo00 = document.forms['form1'].elements[formField[0]];
if (!(/^[0-9a-zA-Z\s\,\/\-\.]+$/).test(campo00.value)) {
alert(str_error_01a + formField[0] + str_error_01b);
campo00.focus();
return false;
}
.............
it not accept a '' text null, what i fix it? is probably that error is in
regular expression, what is correct regular expression? Thanks you,
Cooper.