By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Use onblur to check whether numeric

P: 170

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
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 5,369
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

P: 170
Brilliant. Thanks gits. So easy!
Apr 27 '09 #3

Expert Mod 5K+
P: 5,369
:) 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.