?
Solved

How to determine if a javascript string contains a number

Posted on 2016-10-21
8
Medium Priority
?
97 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 58

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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
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 58

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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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

765 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