• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Javascript validation for numeric or null

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
Mitch Swetsky
Asked:
Mitch Swetsky
1 Solution
 
leakim971PluritechnicianCommented:
replace : if(inputtxt.value.match(numbers))
by : if(inputtxt.value.match(numbers)||inputtxt.value.length==0)
0
 
Ken ButtersCommented:
How about this :

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

Open in new window

0
 
Mitch SwetskyBusiness AnalystAuthor Commented:
Thank you for the prompt reply. that works like a charm!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now