Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an additional problem with the function:

JavaScript
function checkInp() {
var input = document.getElementsByTagName("input");
for (var i =0; i < input.length; i++) {
if (input[i].type == "text") {
if (isNaN(input[i].value)) {
alert("Not Number");
return false;
}
}
}
}


this function works fine till I have to input negative numbers and decimal numbers which I separate them with comma (,) ex: -12.3 or -3,4 ... How can I exclude comma( ,) and minus(-) to recognize them as characters?
Posted
Updated 28-Nov-14 0:40am
v4

Hi Can you please elaborate more, what exactly you required. Do you need to put only characters or numbers or any combinations.
 
Share this answer
 
Try regex then:
C#
function checkInp() {
var input = document.getElementsByTagName("input");
for (var i =0; i < input.length; i++) {
    if (input[i].type == "text") {
       var str = input[i].value;
       var pattern = /^[-]?(\d+(?:[\.\,]\d+)?)$/g;
       var result = pattern.test(str);
       if (!result) {
          alert("Not Number");
          return false;
       } else {
         alert(input[i].value);
       }
    }
}
}

Read more: JavaScript RegExp Reference[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900