Solved

Javascript validation for numeric or null

Posted on 2013-05-10
3
313 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
Comment Utility
replace : if(inputtxt.value.match(numbers))
by : if(inputtxt.value.match(numbers)||inputtxt.value.length==0)
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
How about this :

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

Open in new window

0
 
LVL 1

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now