0

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

Thanks!

6
  • 3
    How exactly are you using that function? Commented Nov 4, 2013 at 15:08
  • So sorry, it's used to validate entries before submitting it to an sql database, with an alert Commented Nov 4, 2013 at 15:10
  • do you want just positive integers? positive and negative? floating point numbers as well? scientific notation? Commented Nov 4, 2013 at 15:10
  • I understand that. I meant, in your code on your page, how and where is that function called? Commented Nov 4, 2013 at 15:11
  • Change if(inputtxt.value.match(numbers)) to if(!numbers.test(inputtxt.value)) which will match non numbers Commented Nov 4, 2013 at 15:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much Megawac for your response, i have updated the question with the code you supplied but for some reason it just submits the data without any warning (when entering non-numeric characters), thanks!
Does inputtxt refer to the correct element? Can you set up a jsfiddle if code continues not working
Updated but that fiddle doesnt work as the validateForm function is not defined when it's added to the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.