By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

Prompt and form

P: n/a
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.
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.