onblur and submit - called validation twice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buntyindia
    New Member
    • Jun 2007
    • 101

    onblur and submit - called validation twice

    Hi,

    I have a form in which there is a field email

    I have to test its valid format on field onblur event and on form submit.

    Now for example
    I have enter something invalid in the email text box and press tab ,
    Now the onblur function works well and show alert message once that "invalid format"
    but when I press enter on the email field instead of tab it shows the validation alert twice, means call both onblur and submit validation function.

    Please help me in this


    Regards,
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You shouldn't really have an alert onblur. Use the DOM to show/insert an error div next to the element instead.

    The enter key press triggers a submit by default in many browsers when in a field.

    Comment

    • buntyindia
      New Member
      • Jun 2007
      • 101

      #3
      Originally posted by acoder
      You shouldn't really have an alert onblur. Use the DOM to show/insert an error div next to the element instead.

      The enter key press triggers a submit by default in many browsers when in a field.
      acoder I am doing something like below
      Code:
      <html>
      <head>
      <script language='JavaScript' type='text/JavaScript'>
      function somealert(some)
      {
          alert('I am Called ' + some);
      }
      </script>
      <title>Sample Code</title>
      </head>
      <body>
      <form onsubmit="somealert('Submit')">
          <input name="textfield" type="text" onblur="somealert('field1')"><br>
          <input name="textfield2" type="text" onblur="somealert('field2')"><br>
          <input type="submit" value="Submit">
      </form>
      </body>
      </html>

      I have to use only alert not div. Please tell me any alternate about this approach. I have to check for pattern format on lost focus + on submit.

      When I am pressing enter on field two alert box are calling..it should be only one :(

      Regards,

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Well, if you insist on using an alert, you're going to have to capture the key entered in the fields and deal with the enter key. The question is do you want to prevent form submission when the enter/return key is pressed?

        Comment

        • buntyindia
          New Member
          • Jun 2007
          • 101

          #5
          Originally posted by acoder
          Well, if you insist on using an alert, you're going to have to capture the key entered in the fields and deal with the enter key. The question is do you want to prevent form submission when the enter/return key is pressed?
          I only want, when enter is pressed on the textbox only onsubmit is called onblur/onchange should be ignored.

          onblur/onchange works when tab is pressed.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Are you calling the exact same function onblur/onchange and onsubmit? onsubmit is for the whole form and onblur would be for one field.

            One possibility if you insist going down this route is to check for the enter key press in the function called. If so, ignore the validation for onblur. Another is to set a variable when the function is called. Yet another is to have two separate functions. I still think that you should avoid alerts onblur.

            Comment

            • buntyindia
              New Member
              • Jun 2007
              • 101

              #7
              Originally posted by acoder
              Are you calling the exact same function onblur/onchange and onsubmit? onsubmit is for the whole form and onblur would be for one field.

              One possibility if you insist going down this route is to check for the enter key press in the function called. If so, ignore the validation for onblur. Another is to set a variable when the function is called. Yet another is to have two separate functions. I still think that you should avoid alerts onblur.
              Both are little different. on blur I am only checking for pattern (data is entered in right format) but on enter (while submitting form) i am validating required check including format.

              This is the requirement by client :(

              Any example available ?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                In that case, something you could try is to set a variable when enter is pressed in each text field (that triggers submit) and test for it in the function called onblur. If set, then don't validate there and leave it to the function called onsubmit.

                Comment

                Working...