[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to determine if a javascript string contains a number

Posted on 2016-10-21
8
Medium Priority
?
109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 59

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 6

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 59

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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

650 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