Prompt and form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cooper

    #1

    Prompt and form

    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.


  • Richard Hockey

    #2
    Re: Prompt and form

    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=For mObject.element s['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(FieldO bject.value)
    {
    // invalid text in field
    alert(str_error _01a + 'fieldname' + str_error_01b);
    FieldObject.foc us();
    return false;
    }
    }

    "Cooper" <cooper@bluewar e.it> wrote in message
    news:WlHPa.3109 8$qa5.684979@ne ws2.tin.it...[color=blue]
    > "Richard Hockey" <richardhockey@ dsl.pipex.com> ha scritto nel messaggio
    > news:3f0e7792$0 $964$cc9e4d1f@n ews.dial.pipex. com...[color=green]
    > > I think you are looking for the javascript focus() method, which sets[/color][/color]
    the[color=blue][color=green]
    > > currently active form element.
    > >
    > > <script type="text/javascript>
    > > function Validate()
    > > {
    > > var FormObject=docu ment.forms['myform'];
    > > var T1Object=FormOb ject.elements['text1'];
    > > var T2Object=FormOb ject.elements['text2'];
    > >
    > > if(T1Object.val ue=='')
    > > {
    > > // display alert
    > > alert('You have not entered any text in field 1. Please enter some[/color][/color]
    now.');[color=blue][color=green]
    > > // set focus on invalid field
    > > T1Object.focus( );
    > > // go back to from WITHOUT submitting
    > > return false;
    > > }
    > >
    > > if(T2Object.val ue=='')
    > > {
    > > alert('You have not entered any text in field 1. Please enter some[/color][/color]
    now.');[color=blue][color=green]
    > > T2Object.focus( );
    > > return false;
    > > }
    > >
    > > // all fields validated, submit form
    > > return true;
    > > }
    > > </script>
    > >
    > > <form name="myform" action=blah.php " method ="post" onSubmit="retur n
    > > Validate();">
    > > <input type="text" name="text1">
    > > <input type="text" name="text2">
    > > <input type="submit" value="Submit form">
    > > </form>
    > >[/color]
    >
    > Ok thanks you, but i have a problem now, if in a text i want to accept[/color]
    also[color=blue]
    > '' 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.
    >
    >[/color]


    Comment

    Working...