Solved

How to determine if a javascript string contains a number

Posted on 2016-10-21
8
56 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
8 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 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 5

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 54

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

803 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