?
Solved

How to determine if a javascript string contains a number

Posted on 2016-10-21
8
Medium Priority
?
119 Views
Last Modified: 2016-10-21
I am using the below code to take all of the values selected in a multi-select box and turn them into a comma separated string. I need to write an IF statement to determine if that comma separated string contains a number. Can someone assist? See my code and comment example below:

	var selectedCarsTypeArray = new Array();

	   	for (i = 0; i < selCarType.options.length; i++) {
			if (selCarType.options[ i ].selected) {
		    	selectedCarsTypeArray.push(selCarType.options[ i ].value);
		   		}
		   }
	    allSelectedCarTypes = selectedCarsTypeArray.join(',');

if ( allSelectedCarTypes==15 ) { //This is where I want to check to see if "allSelectedCarTypes" contains the number 15
}

Open in new window

0
Comment
Question by:earwig75
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41854303
To check if a string has a number use match(/\d+/)

<script>
var x = 'this has 9 as the number';
if (x.match(/\d+/)) {
  console.log('A number was found');
}
else {
  console.log('No numbers here!');
}

var y = 'No number in this string';
if (y.match(/\d+/)) {
  console.log('A number was found');
}
else {
  console.log('No numbers here!');
}
</script>

Open in new window

0
 
LVL 7

Expert Comment

by:Austin Texas
ID: 41854335
Thanks for the heads up, Julian. I didn't know that was a rule and I thought the verbiage in the link was descriptive enough to be clear. I'll provide more explanation in the future.
0
 

Author Comment

by:earwig75
ID: 41854369
Julian, I'm sorry but I am still confused.  I am trying to search the string for a specific number. Would I replace your regular expression with one that contains that number?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:Moussa Mokhtari
ID: 41854449
@earwig75

If you are looking for lets say 2016 in a text you can do like this
var text = "this year 2016 is great"
if(text.match('2016')){
alert("2016 found");
}else{
alert('2016 not found');
}

Open in new window

0
 

Author Comment

by:earwig75
ID: 41854458
Moussa, that wuold also match 20165. I found out I need to use something like this: x.match(/\b15\b/) to match 15 only. Thank you.
0
 
LVL 9

Expert Comment

by:Moussa Mokhtari
ID: 41854467
Sorry for that its a silly mistake :)
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41854661
I am trying to search the string for a specific number.
You need to state this in your question otherwise it will lead to confusion.

When you say a specific number - will it always be the same number or will you need to search for a specific number that may change - in other words user inputs something - you need to check if that number is in a string.

To search for anything in a string just do

x.match(/expr/);

Where expr is what you want to find - it can be a standard string or a regex

so x.matc(/12345/) will find 12345 in the string.

If the search string is dynamic though you would need to create a new RegExp

Example

<script>
var userinput = '12345';
var searchstr = 'This is a test to see if 1234 is in the string';

var re = new RegExp(userinput);
if (searchstr.match(re)) {
  console.log('Found it');
}
else {
  console.log('Not here!');
}
</script>

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question