Prompt and form

C

Cooper

Hello,
i have a form with TEXT, SELECT etc, elements. Simple, i suppose two TEXT
(name: T1 and T2). If a user insert a bad value, it display a error message
and turn back at form.
My problem is: I am in T1, i insert a bad value... is displayed a alert, i
click on OK and prompt go in T2. Now, i want to do that if i am in T1 after
that i click OK in alert window, the prompt turn in T1 and not go in T2.
I hope that i have explained my problem. Someone, help me about it?
Thanks you, Cooper.
 
R

Richard Hockey

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 said:
Richard Hockey said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,076
Messages
2,570,565
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top