Solved

Javascript validation for numeric or null

Posted on 2013-05-10
3
318 Views
Last Modified: 2013-05-10
I have a page with 3 text boxes T1,T2,T3
I want to force the user to enter numeric values or a null

The following function works for the numeric part but I need to also accept if null

  function allnumeric(inputtxt)
   {
      var numbers = /^[0-9]+$/;
      if(inputtxt.value.match(numbers))
      {
      alert('Your new number has been accepted');
      document.Form_Edit.T1.focus();
      return true;
      }
      else
      {
      alert('Please input numeric characters only');
      document.Form_Edit.T1.focus();
        window.history.back()
      return false;
      }  
}
0
Comment
Question by:Mswetsky
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39155494
replace : if(inputtxt.value.match(numbers))
by : if(inputtxt.value.match(numbers)||inputtxt.value.length==0)
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39155498
How about this :

if(inputtxt.value.match(numbers) || inputtxt == null)

Open in new window

0
 
LVL 1

Author Closing Comment

by:Mswetsky
ID: 39155519
Thank you for the prompt reply. that works like a charm!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question