2

How do I test if the string has atleast one numerical character rather than test if the whole string is a number?

This is the current code. It works in the following scenarios: If there is one integer e.g. "43" will display the text corresponding to row 43 in the DB. If there is a plain text e.g. "None" will display the label "none"

When I have more than one integer comma seperates e.g. "43,82" it will display as "43,82" instead of the corresponding rows.

Jquery:

if(!isNaN(parseFloat(gestures[gesture_key].confirming)) && isFinite(gestures[gesture_key].confirming)){ var confirming = gestures[gesture_key].confirming.split(','); for(var i=0;i<confirming.length;i++){ // Find the name of the gesture for(var j=0;j<gestures.length;j++){ if(gestures[j].id == confirming[i]){ var gesture_name_key = j; var absense = false; } if(gestures[j].id == -1 * confirming[i]){ var gesture_name_key = j; var absense = true; } } if(!absense) { $("ul#confirming_gestures").append("<li class=\"gesture\" value="+confirming[i]+">"+gestures[gesture_name_key].name+"</li>"); } if(absense) { $("ul#confirming_gestures").append("<li class=\"gesture\" value="+confirming[i]+">Absense of "+gestures[gesture_name_key].name+"</li>"); } } } else { $("ul#confirming_gestures").append("<li>"+gestures[gesture_key].confirming+"</li>"); } 

1 Answer 1

3

There is a very simple way to test for the presence of at least one numerical character:

/\d/.test("asdf"); // false /\d/.test("as3df"); // true /\d/.test("12345"); // true 

So to generalize, you can run any javascript variable and get a boolean result.

if(/\d/.test(input)) { alert('has at least one digit'); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.