468,321 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Use onblur to check whether numeric

170 100+

I have a number of text boxes for the user to input either numbers, or letters. Sometimes, the entry must be numeric. So I was thinking of using an onblur thing to check whether the value of the input is numeric, and if not, show an alert box to the user.

I assume I would need to use getelementbyid. And on this page, there might be up to 30 similar input text fields, all of which need to be checked when the focus is lost. But obviously, they would have different ids, and I don't want to have to have 30 different javascript functions to check. Is there a way around this?

To summarise, as the user enters something into the text field, when they click somewhere else, I need to check that entry to see if it's numeric, and I need to do it for as many text fields as there are on the page.

I am not remotely a javascript expert.
Apr 26 '09 #1
3 6449
5,390 Expert Mod 4TB
just make use of the this keyword here that refers to the node itself:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function validateNumericValues(node) {
  5.     alert(node.value);
  6. }
  7. </script>
  8. <body>
  9.     <input type="text" id="foo" onblur="validateNumericValues(this);"/>
  10. </body>
  11. </html>
kind regards
Apr 26 '09 #2
170 100+
Brilliant. Thanks gits. So easy!
Apr 27 '09 #3
5,390 Expert Mod 4TB
:) no problem ... that's what we're here for ;) ...

kind regards
Apr 27 '09 #4

Post your reply

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

Similar topics

2 posts views Thread by Eric | last post: by
6 posts views Thread by WindAndWaves | last post: by
5 posts views Thread by EviL KerneL | last post: by
3 posts views Thread by Robert Oschler | last post: by
2 posts views Thread by Heiko Vainsalu | last post: by
10 posts views Thread by John Kotuby | last post: by
7 posts views Thread by laredotornado | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.