How to check digit from string [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have getting string. I want to check string have any digit.
The string may be -
var val="1"; or val=",,"; or val = "1,2,0";
I want to check if val have any digit.

function hasDigit(yourString) {
return yourString.match(/\d/) !== null;
}
Here's a fiddle demo: http://jsfiddle.net/VpLgG/

You can use yourInputString.match(/[0-9]/g), or what Jonah has specified. This will return null if there are not digits else it will return the values present inside the string that will match separated by comma.

Javascript side :
if (/\d/.test(val)) {
// do something
}
Not really useful but required to get attention apparently (demo here)...
function hasDigit(val) {
return /\d/.test(val);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Related

How to detect only digit ,'(' , ')' ,'+' in Javascript String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I would like to detect if a JavaScript String contains one of these following characters :
a digit ( 0 to 9)
the '(' character
the ')' character
How can i do that ?
var s = 'your string';
if (/[0-9()+]/.test(s)) {
// match
}
0-9 can be written as \d
if (/[\d()+]/.test(s)) {}
Your question is inconsistent. If you want to test if string is '0' or '1' ... or '(':
if (/^[\d()+]$/.test(s)) {}
Read about RegEp here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

How to get total number of vowels in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am just curious how you can get the total number of vowels in a string. I know this is a very simple function, but I am new to Javascript. Thanks for the help
You can remove all characters that are not vowels and count the remaining ones:
var str = /* something*/,
numberVowels = str.replace(/[^aeiou]/ig, '').length;
Where
/[^aeiou]/ matches single character NOT present in the list "aeiou"
i is the case-insensitive flag, to count AEIOU too.
g is the global flag, to replace ALL characters which are not vowels, instead of just the first one.

Remove () and - and white spaces from phone number in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a phone number like (123) 456-7891. I need number like 1234567891.
How can I do that in Javascript ?
To be on the safe side, I would recommend removing everything except + (for country codes) and digits:
result = subject.replace(/[^+\d]+/g, "");
You can use String.replace() to do this. Pass it a RegExp that matches everything but digits and replace them with ''.
var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, '');
alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));
please check this link.it might help you
http://www.w3resource.com/javascript/form/phone-no-validation.php
there are many examples here.it might be useful for you.

How to replace number in this string using JavaScript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can someone tell me how to replace the number "2" with another number in this string"? For example, if it's a 2, it should be a 3, if it's a 3 it should be a 4, etc.
Please note that the number can be any number from 1 through 5.
/img/tmp/2_th.jpg
Just use String#replace:
s.replace(/\d+(?=_)/, "replaced");
To replace any number from 1-5 use:
s.replace(/[1-5](?=_)/, "replaced");
Here (?=_) is used a positive lookahead which makes sure match a number which is followed by underscore _.
UPDATE: Based on your edit you can use this code to increment matched number by 1:
s.replace(/([1-5])(?=_)/, function(n) {return parseInt(n)+1;});
Read More About Lookarounds in Regex
I understand from your question that you want to replace any number by the following one (" if it's a 2, it should be a 3, if it's a 3 it should be a 4").
Then you can do this :
var input = "/img/tmp/2_th.jpg";
var output = input.replace(/\d+/g,function(s){ return +s+1 })
Result :
"/img/tmp/3_th.jpg"

Get the Particular part of the textbox ID in jquery [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the Text box .That text Box ID is text_1__val.
I need 1 from that Id.How to Get the Particular part of the textbox ID in jquery which means i need the between _ and __ from that textbox ID?
If the only requirement is that the character / characters appear between a single and double underscore, try this regular expression match
var rx = /_(.+?)__/;
var part = rx.test(idValue) && rx.exec(idValue)[1];
This assumes that you're only after the first of any occurrences in your ID value string. If the string fails to match, part will be false.
The split() method is used to split a string into an array of substrings, and returns the new array.
$(your_textbox).attr("id").split("_")[1]
//Syntax
string.split(separator,limit)
Function Reference: http://www.w3schools.com/jsref/jsref_split.asp

Categories

Resources