473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

onblur and submit - called validation twice

101 100+
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,
Sep 12 '08 #1
7 9948
acoder
16,027 Expert Mod 8TB
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.
Sep 12 '08 #2
buntyindia
101 100+
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
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language='JavaScript' type='text/JavaScript'>
  4. function somealert(some)
  5. {
  6.     alert('I am Called ' + some);
  7. }
  8. </script>
  9. <title>Sample Code</title>
  10. </head>
  11. <body>
  12. <form onsubmit="somealert('Submit')">
  13.     <input name="textfield" type="text" onblur="somealert('field1')"><br>
  14.     <input name="textfield2" type="text" onblur="somealert('field2')"><br>
  15.     <input type="submit" value="Submit">
  16. </form>
  17. </body>
  18. </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,
Sep 14 '08 #3
acoder
16,027 Expert Mod 8TB
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?
Sep 14 '08 #4
buntyindia
101 100+
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.
Sep 14 '08 #5
acoder
16,027 Expert Mod 8TB
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.
Sep 14 '08 #6
buntyindia
101 100+
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 ?
Sep 15 '08 #7
acoder
16,027 Expert Mod 8TB
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.
Sep 15 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Roger Withnell | last post by:
Tearing hair out time! Simple attached page shows the problem. http://www.brilley.co.uk/TestFocusSelect.htm Using a function to test if too many characters have been keyed in to a textarea....
1
by: Christoph | last post by:
I'm trying to validate some HTML form elements when the user tabs out of each element. However, I'm having some problems. It appears that the order of events is onChange followed some time...
4
by: Peloux | last post by:
Hi, I have written some htc in order to validate data in a form. most of htc are attached on 'onblur' event. Now, we would like to use the Enter Key to sublit form, so we use the following...
5
by: Dave Hammond | last post by:
Hi All, I have a web form which performs certain actions upon moving focus away from a field. However, if the user clicks the top corner 'X' icon to close the window, the onBlur event still...
2
by: Heiko Vainsalu | last post by:
Hi Hope somebody knows how to solve this one. *The Situation* A traditional situation where HTML form inputs are checked... (if simplified then it would look something like this) <form...
4
by: josh.dutcher | last post by:
I'm trying to set up a simple "change your password" form. I've got an AJAX process that is checking the new password the user enters against their current password to make sure they're changing...
2
by: hrpreet | last post by:
Hi All, I have few textfields and dropdowns in my jsp, form is submitted on clicking an image link. In case there is an error for instance some text field is left empty, then message is...
0
by: =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?= | last post by:
I'm having trouble working with a Summary Validation control. I need to click my submit button twice for the Summary to display when validation errors occur on the page. Background: My page...
0
by: josephkorn | last post by:
Hi all. I have a problem in my website in trying to prevent a user from double submitting the form. I am calling a subroutine from my page_load event that passes in the commandbutton that I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.