How to check if the URL contains a number? [duplicate] - javascript

This question already has answers here:
Check whether an input string contains a number in javascript
(14 answers)
Closed 2 years ago.
I am using window.location.href.indexOf to check if URL contains a string and works very well for something like this:
if (window.location.href.indexOf("franky") > -1) {
alert("your url contains the name franky");
But it doesn't work to check if URL contains any number.
The following always calls the alert, even if no number is in the URL.
if (
window.location.href.indexOf("0") === -1 ||
window.location.href.indexOf("1") === -1 ||
window.location.href.indexOf("2") === -1 ||
window.location.href.indexOf("3") === -1 ||
window.location.href.indexOf("4") === -1 ||
window.location.href.indexOf("5") === -1 ||
window.location.href.indexOf("6") === -1 ||
window.location.href.indexOf("7") === -1 ||
window.location.href.indexOf("8") === -1 ||
window.location.href.indexOf("9") === -1
)
{ alert("false"); }

As gaetanoM suggested, a regular expression would be the easiest way to do it.
if (window.location.href.match(/\d/)) {
alert('contains a number');
} else {
alert('does not contain a number');
}

Related

Use .indexof to Check for Multiple Strings with Array [duplicate]

This question already has answers here:
How to check if a string contains text from an array of substrings in JavaScript?
(24 answers)
Closed last year.
I am attempting to embed a chat widget if utm_source=VALUE (any of these values)
Here is the surrounding code:
var ref1 = document.referrer;
IF STATEMENT HERE
{
alert("Welcome to Ellas Bubbles");
s1.src='https://embed.tawk.to/620675459bd1f31184dc28c0/1frkjk5mj';
}
You can test it here: https://ellasbubbles.com
I have tried the following, which is only working on 'ccov':
if ((ref1.indexOf('ccov') || ref1.indexOf('top10') || ref1.indexOf('cenf') || ref1.indexOf('aip') || (getcookie('track-page-1').indexOf('ccov')>-1) || (getcookie('track-page-1').indexOf('top10')>-1) || (getcookie('track-page-1').indexOf('cenf')>-1) || (getcookie('track-page-1').indexOf('aip')>-1)) > -1)
I have also tried this:
if ((ref1.indexOf('ccov' || 'top10' || 'cenf' || 'aip') || (getcookie('track-page-1').indexOf('ccov' || 'top10' || 'cenf' || 'aip')>-1)) > -1)
{
s1.src='https://embed.tawk.to/620675459bd1f31184dc28c0/1frkjk5mj';
}
Which is also only working on the first value, 'ccov'.
I have also tried this:
if ((ref1.indexOf('ccov', 'top10', 'cenf', 'aip') || (getcookie('track-page-1').indexOf('ccov', 'top10', 'cenf', 'aip')>-1)) > -1)
Which is also only working on the first value
Is there a better way to go about this?
I think your statements are incomplete. Before each || you need to condition to be satisfied. I’m writing on my phone so sorry this isn’t as specific but you need the >-1 before every ||. So instead of
X || Y < z
I think You need
X < z || y < z.

If url has index of x or y then do not do the following

I think I am having trouble with my syntax on this. I want it to say if the url has indexof 425 or if the url has an indexof 297 do not run the following. This works for doing just one:
if (document.location.href.indexOf('425') === -1){
But when I try to add the second indexof it doesnt work, here is what I have tried
//attempt 1
if (document.location.href.indexOf('425') === -1 || document.location.href.indexOf('297') === -1){
}
//attempt 2
if ((document.location.href.indexOf('425')) === -1 || (document.location.href.indexOf('297')) === -1)){
}
I want it to say if the url has indexof 425 or if the url has an indexof 297 do not run the following.
Or to put it another way, if the url doesn't have 425 and doesn't have 297, do the following:
if (document.location.href.indexOf('425') === -1 && document.location.href.indexOf('297') === -1){
=== -1 means it wasn't found.
But these days, you can use includes (polyfilling for IE if you need to support IE):
if (!document.location.href.includes('425') && !document.location.href.includes('297')){
You need a logical AND &&, because both parts have to be true
if (
document.location.href.indexOf('425') === -1 &&
document.location.href.indexOf('297') === -1
) {
// ...
}
For more than one value, you could take an array with the unwanted parts and use Array#every for checking.
if ['425', '297'].every(s => !document.location.href.includes(s))) {
// ...
}

Combine && and || on javascript

I wish to say if URL has 'admin/deals' and 'edit' at the same time in the current url page OR if it contains 'deals/new', then DO something.
if ( (window.location.href.indexOf('admin/deals') > -1)
&& (window.location.href.indexOf('edit') > -1) ) )
|| if (window.location.href.indexOf('deals/new') > -1){
alert("your url contains the name franky");
}
But I get the following error:
SyntaxError: expected expression, got ')'
the syntax is:
if (condition) || (condition)
^---no "if" here
so
if ((a && b) || (c && d)) { ... }
You can't have an if expression inside of another if expression. Try this:
if ((window.location.href.indexOf('admin/deals') > -1 && window.location.href.indexOf('edit') > -1) || (window.location.href.indexOf('deals/new') > -1)){
alert("your url contains the name franky");
}
You don't need two if's - you just combine all your conditions into one if and add as many && and || as you need.
You should also ensure for every open ( there is a closing )
if (window.location.href.indexOf('admin/deals') > -1 &&
window.location.href.indexOf('edit') > -1 ||
window.location.href.indexOf('deals/new') > -1) {
alert("your url contains the name franky");
}
And you might want to introduce a variable for window.location.href
like var currentLocation = window.location.href;
and then use that variable ...

What's the easiest way to check if a javascript variable is a number and greater than zero? [duplicate]

This question already has answers here:
Validate decimal numbers in JavaScript - IsNumeric()
(52 answers)
Closed 8 years ago.
I coded this:
isNumberNotZero: function (num) {
// Return false if num is null, an empty string or zero
if (num === null || typeof num === "undefined" || (typeof num === "string" && num.length === 0) || num == 0) {
return false;
}
return true;
},
But is there an easier way to do this. The code I have does not seem very clean.
You are doing a lot of checking for things that it is not, when you only care if it is a number greater than zero.
Just:
return typeof a === "number" && a > 0
This is from an answer to the famous Validate numbers in JavaScript - IsNumeric() question:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Then your test is as simple as combining that function with a > 0:
return isNumber(input) && input > 0;
You could use a unary plus operator to convert to an integer, if you want to shorten the code.
This isn't as readable, and only works for integers (e.g., if your number is 0.5, this will treat it as a zero), but it's fast and concise.
function isNumberNotZero(num) {
return +num !== 0;
}

what is the difference between != and !== in Javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Are != and !== are respectively the same as == and ===?
!== and === are strict comparison, and == / != are loose comparison. It's best to use strict comparison.
true == 1 gives you true
true === 1 gives you false
Reason is that == compares only the value (so that 1, '1' is considered as true)
=== compares the value and the type.
Same thing in PHP.
== compares the value of object while === compares the object value and type.
yes it is.
<script>
var str = '1234';
var int = parseInt('1234');
if (int !== str)
{
alert('returns true and alerts');
}
if (int === str)
{
alert('returns false');
}
</script>
http://sandbox.phpcode.eu/g/c801e.php

Categories

Resources