Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8500393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:55:59+00:00 2026-06-11T00:55:59+00:00

I have several input fields that should have only numbers as valid input, so

  • 0

I have several input fields that should have only numbers as valid input, so after search the SO, I got the following js code to allow only number to be input to a text field which is working quite well.

function onlyNumbers(evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode( key );
    var regex = /[0-9]|\./;
    if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();

        // the corresponding span to make it visible here
        // What code to put here ?

    }
}

The corresponding html is

<input type="text" id="age1" onkeypress="onlyNumbers(event)" />
<input type="text" id="mobileNo" onkeypress="onlyNumbers(event)" />

Now, I have a hidden span after each such input text, which I would like to display as soon as a invalid character is typed ? As there are many such fields

e.g. age1, age2, mobileNo, each one has it’s own sibling span.
So, when age1 input is wrong it’s corresponding error message span should get displayed.

Here is the minimal complete html code for reference

<form method="POST" action="action.php">
    ...
<li>Age of person 1 :
    <input type="text" id="age1" onkeypress="onlyNumbers(event)" />
    <span class="error_msg age1"> Only numbers please </span> 
</li>
<li>Age of person 2 :
    <input type="text" id="age2" onkeypress="onlyNumbers(event)" />
    <span class="error_msg age2"> Only numbers please </span> 
</li>
<li>
    <input type="text" id="mobileNo" onkeypress="onlyNumbers(event)" />
    <span class="error_msg mobileNo"> Only numbers please </span> 
</li>
...
</form>

How to get the reference of the element from which the onlyNumbers() function was called? And how to make the corresponding span visible? Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:56:01+00:00Added an answer on June 11, 2026 at 12:56 am
     var fld = theEvent.target || theEvent.srcElement;
    

    Here you can access ID or sibling node

    If you instead or as well pass (this) you have your field

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, we have the following problem: we've got several objects containing table data that
I have several datepickers tied to input fields. They are created in the usual
In my iOS app I have several UIElement s that can process user input:
I'm developing an app that should allow the client to add several content sections
I have several applications within my domain that accept similar inputs in text fields.
I have a several input fields in a form each having a unique name.
Just a basic question: If I have several dozen EditText fields that are part
I have a form in CakePHP that has two live-search text input. Each one
Hi I have several text input boxes, and i have a clear button, they
I have... a dynamic populated select box several input boxes a submit button form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.