ONBLUR() LOOP

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

    #1

    ONBLUR() LOOP

    I do validate my form using onblur() event.

    In my example I have 3 fields in my form.
    name,address and city. I call my functions using onblur()="isnam e(this)"
    ...etc. My probllem is that when I fill the form fields everything is
    fine. When I dont I have a problem. When I leave empty field for lets say
    name the the isname() function is called and tells my to enter the data.
    It tries to focus on that field and select it. The problem is that when I
    change the focus from name to the second field address the second script
    is already running. So when I click OK on my alert message I cant fill in
    the field because second field validation function runs. So I am in the
    loop. WHY? Is there any way I can stop the computer to focus on next
    field when I press tab or click on the next field. Whats wrong.
    Thats my code.

    function isname(field) {
    if (field.value == "") {
    alert("Value is required");
    field.focus();

    field.select();
    }
    else {
    if (field.value.sp lit(" ").length < 2) {
    alert("Enter a full name");
    field.focus();

    field.select();
    }
    }
    }

    function isaddress(field ) {
    if (field.value == "") {
    alert("Value is required");
    field.focus();
    field.select();
    }
    }

    function iscity(field) {
    if (field.value == "") {
    alert("Value is required");
    field.focus();
    field.select();
    }
    }


    Thanks a lot

  • Jerry Park

    #2
    Re: ONBLUR() LOOP

    Bartosz Wegrzyn wrote:[color=blue]
    > I do validate my form using onblur() event.
    >
    > In my example I have 3 fields in my form.
    > name,address and city. I call my functions using onblur()="isnam e(this)"
    > ..etc. My probllem is that when I fill the form fields everything is
    > fine. When I dont I have a problem. When I leave empty field for lets say
    > name the the isname() function is called and tells my to enter the data.
    > It tries to focus on that field and select it. The problem is that when I
    > change the focus from name to the second field address the second script
    > is already running. So when I click OK on my alert message I cant fill in
    > the field because second field validation function runs. So I am in the
    > loop. WHY? Is there any way I can stop the computer to focus on next
    > field when I press tab or click on the next field. Whats wrong.
    > Thats my code.
    >
    > function isname(field) {
    >
    > if (field.value == "") {
    >
    > alert("Value is required");
    >
    > field.focus();
    >
    >
    >
    > field.select();
    >
    > }
    >
    > else {
    >
    > if (field.value.sp lit(" ").length < 2) {
    >
    > alert("Enter a full name");
    >
    > field.focus();
    >
    >
    >
    > field.select();
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    > function isaddress(field ) {
    >
    > if (field.value == "") {
    >
    > alert("Value is required");
    >
    > field.focus();
    >
    > field.select();
    >
    > }
    >
    > }
    >
    >
    >
    > function iscity(field) {
    >
    > if (field.value == "") {
    >
    > alert("Value is required");
    >
    > field.focus();
    >
    > field.select();
    >
    > }
    >
    > }
    >
    >
    > Thanks a lot
    >[/color]
    Onblur() has lots of problems as you noted. It is not really suitable
    for that purpose.

    Try using onchange instead.

    Comment

    Working...