Solved

How to determine if a javascript string contains a number

Posted on 2016-10-21
8
34 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 51

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 51

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now