user2680550
user2680550

Reputation:

Numeric only Javascript form Validation?

Can't seem to get this function to work for some reason, read about 10000 documents lol

Thanks!

Upvotes: 0

Views: 2808

Answers (1)

megawac
megawac

Reputation: 11373

You just have a bit of poor logic with your if statement for numbers. As I mentioned in the comment

if(inputtxt.value.match(numbers)) should be

if(!numbers.test(inputtxt.value)) {
      alert('Please input numeric characters only');
      document.reasoning.mpn.focus();
      isValid = false;
}

For your document you're trying to validate fields with mpn name but you're not retrieving them as far as I can see. Seeing there is only 1 mpn field you may want to use an id so you dont have to iterate as below. Try the following:

isValid = isValid && all(document.getElementsByName("mpn"), function(ele) {
   if(numbers.test(ele.value)) {
       return true;
   } else {
       alert('Please input numeric characters only');
       ele.focus()
       return false;
   }
});

I don't feel like typing out the code for all but assume its something like _.all from underscorejs.

Upvotes: 2

Related Questions