identifying numbers using javascript - javascript

How can I make a function that takes a string and checks if it is a number string or if it includes letters/other characters? I have no idea what to do... RegExp takes too long so is there another way?

You have to use isNaN() function (is Not a Number). It will return you true if it's not a number (that mean that it contains letter) and false if it's one.
Source :
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN

You can check for isNaN and see if value is number if you don't want to go with RegExp.
let inpArgs = Number.parseInt(input);
if(!Number.isNaN(inpArgs)){
// this check ensures that your input is number
// Do what you have to do
}
else{
// Handle the error
}
But I would prefer the one line check using RegExp any day like below.
if(/^\d+$/.test(Number(input))){
// this says your input is Number
}

You can use typeof oprator to check whether it is a number or string.
function(anything){
if(typeof(anything)==='number'){ //do something} }
if(typeof(anything)==='string'){ //do something} }
Hope I answer your question.
Thanks.

You can use typeof in JavaScript to identify input Like
alert(typeof <your input>); or var identity = typeof <your input>;
and whatever string alert that match in Condition Like
if (identity == <alert message>){do something}else{do something}

Related

How to compare string variable using JavaScript

I am trying to compare the variable using javascipt:
response value: ""test#gmail.com""
response value i am getting it from server.
var str1="test#gmail.com"
var str2 =response;
if(str1===str2)
{
//
}
However not getting the proper result.
any idea on how to compare them ?
There are a few ways to achieve your goal:
1) You can remove all " from the response when doing your equality check:
if(str1===str2.replace(/['"]+/g, ''))
{
//
}
2) Change your server code to not include ". Doing so, would mean that your Javascript will not need to change.
3) Last option, add " to your str1:
var str1='"test#gmail.com"'
var str2 =response;
if(str1===str2)
{
//
}
Obviously I don't know enough about your requirements to tell you which one you should do, but my suggestion would be choice #2 because I think it's strange to return an email address wrapped in quotes, otherwise I would recommend #1.
You are trying to compare '""test#gmail.com""' with 'test#gmail.com'. They would never be equal.
Actually ""test#gmail.com"" is not a valid string. It might have been represented as '""test#gmail.com""' and "test#gmail.com" is a valid string (Same as 'test#gmail.com').

Match Strings with Jquery

if(kword == term){
$(this).trigger('click');
}
The case is if the kword is "car" and the term is "cars", I would want that to be a positive match.
Currently I'm looking at an exact match. As I'm a novice at jquery I don't know how to do this. Can anyone point me to the right direction?
You can use indexOf() to find the string inside another
if(term.indexOf(kword)>-1){
//code
}
If you need to simply compare a string variable with a different string variable with an 's' in the end, you can go with following code:
if (term === kword + 's') {
...
}
If you need to check specifically if terms is a plural version of kword, you would need much larger implementation, featuring pluralize function (that you need to implement or search for a library):
if (term === pluralize(kword)) {
...
}
function pluralize(word) {
//implement function
}
If that doesn't not answer your question, please be more clear about what you need to do.
As #Anton said, you can use indexOf(), it returns the position of the first occurrence of a specified value in a string otherwise returns -1 if the value to search for never occurs. Also note that this method is case sensitive.

how to simplify my conditional statement?

I am trying to simplify the following codes. The codes seem to redundant to me. Are there anyone here can help me out? Thanks a lot!
if(area.regionCode=='0' || area.regionCode==null){
var fakecode=area.region.substring(0, area.region.length - 1);
area.region= fakecode +i;
}
Whenever you think some code is not directly revealing, try giving it a new home with a suitable name:
if (!isValidRegionCode(area.regionCode)) {
...
}
...
function isValidRegionCode(regionCode) {
return area.regionCode != null && area.regionCode != '0';
}
It has more code overall, but makes your intentions clear.
if(parseInt(area.regionCode) > 0) {}
I would recommend explicit condition checks. When using:
if (area.regionCode) { }
Style of logic, one is treating varAny as a boolean value. Therefore, JavaScript will perform an implicit conversion to a boolean value of whatever object type varAny is.
or
if(Boolean(area.regionCode)){
codes here;
}
both will work same
returns false for the following,
null
undefined
0
""
false.
beware returns true for string zero "0" and whitespace " ".
you can also first trim the output so " " issue will be solve
here tutorial How do I trim a string in javascript?
in the #mttrb and #nnnnnn described case you can first convert string to either int or float by parseInt() and parseFloat() check this Converting strings to numbers

Check validation for numbers only

following is my piece of code which checks if the input format is EMAIL which comes from the form input or not and validates accordingly i want to know how can i modify the following code that validates if the input was only number
if(email.length == 0 || email.indexOf('#') == '-1'){
var error = true;
$('#email_error').fadeIn(500);
}else{
$('#email_error').fadeOut(500);
}
Use jQuery's IsNumeric method.
http://api.jquery.com/jQuery.isNumeric/
$.isNumeric("-10"); // true
if (email.match(/[0-9]+/)) {
//it's all numbers
}
EDIT
As mentioned in the comments, to ensure that the entry is ALL numbers, the regex would have to include the begin and end characters, ^ and $:
if (email.match(/^[0-9]+$/)) {
//it's all numbers
}
Or even more succinctly:
if (email.match(/^\d+$/)) {
//it's all numbers
}
I don't deserve the credit for that fix, but I did want to correct it for anyone who may come find this later.
I would use:
if(isNaN(email*1)){
//evaluated to NaN
}else{
//evaluated to number
}
In this case the (email*1) have the possibility to evaluate to NaN, and thus will fail the check because the list of falsish values are 0,"",false,null,undefined,NaN
Check if converting the email address to a Number object returns a 'Not a Number' value; if not, the input was a number. The code would look like this:
if(!isNaN(Number(email)) {

jquery check is string is inside a string

I got 2 variables;
value = 'com';
longString= "com-233-123-232-123";
I'd like to check if "value" is inside "longString". I tried using regex with test() but I fail, maybe you know better.
I think the indexOf(substr, [start]) is enough no need to regex.
indexOf(substr, [start])
Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is 0.
http://www.javascriptkit.com/javatutors/string4.shtml
Two ways
1st indexOf
if (longString.indexOf(value) != -1)
// found
else
// not found
2nd split
var value = 'com'; var longString= "com-233-123-232-123";
var split1=longString.split("-");
var i=0;
var found=0;
while (i<split1.length)
{
if(split1[i]==value)
{
found=1;
break;
}
i++;
}
if(found==1)
//found
else
//not found
Don't use regular expressions for this - if the value you're looking for can be interpreted as a regular expression itself then you'll have trouble. Just check for longString.indexOf(value) != -1.
What does jQuery has to do with this? This is a simple Javascript problem
if (longString.indexOf(value) != -1)
// We found it
else
// We didn't find it

Categories