This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript compare strings without being case sensitive.
I am having two fields. I need to check whether both the fields contain the same value. If the same value then a alert is raised.
I am using validation engine jQuery.
I have a written a function like:
function kkkk(field, rules,i,options){
var password=field.val();
var username = getValueUsingElementID('username');
if(password==username){
return options.allrules.NotEqual.alertText;
}
}
Example:
sss2# and sss2# // shows error message
sss2# and SSS2# // no error message is shown
Please can anyone help. Both the values are user input.
Use the toLowerCase() function. That will convert your strings to all lower case characters, and the comparison will then return true whether or not there were differences in case originally:
if(password.toLowerCase() == username.toLowerCase()) {
//Do stuff
}
Convert both in lover case by and than compare
if(password.toLowerCase() == username.toLowerCase())
{
//sucess
}
Related
This question already has answers here:
Contains case insensitive
(14 answers)
Closed 1 year ago.
Example: Dota is better than league of legends (true).
Counter Strike is better than Fortnite (false)
I couldn't develop the line of reasoning, could you help me? Thanks.
The simplest way is this:
function findDota (string){
if (string.toLowerCase().search("dota") != -1){
return true;
} else{
return false;
}
}
string.search() returns the position (0 indexing) of the first occurence of the search parameter in the string. If the search parameter isn't in the string it returns -1.
A more complex answer could use regular expressions (regexs) which can be very powerful.
function findDota (string){
if (string.search(/dota/i) != -1){
return true;
} else{
return false;
}
}
here /dota/i searches for the string dota. The i means it is case insensetive.
useful links:
https://www.w3schools.com/jsref/jsref_search.asp
https://www.w3schools.com/js/js_regexp.asp
https://www.w3schools.com/js/js_string_methods.asp
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 2 years ago.
I'm currently working on a cms that will have an alert appear on multiple pages. I am currently using an if statement to have the alert appear only a page with a specific page title. Is there a way of generalizing it and having it appear on all articles with the word "Test" in the title?
At the moment my logic is if #pageTitle === "Test Article Two display....
I tried doing #pageTitle === "Test" but that only shows on the article that has the title Test rather then other titles with the word Test in them.
Here is my code :
<script>
if(document.title === "Test Article Two") {
document.body.classList.add("show-alert");
}
</script>
Methods -
Regex, case sensitive:
if (/Test/.test(document.title)) { ... }
Regex, case insensitive
if (/test/i.test(document.title)) { ... }
indexOf, case sensitive, (fastest)
if (document.title.indexOf("Test") !== -1) { ... }
includes (ES6), case sensitive
if (document.title.includes("Test")) { ... }
You can use the JavaScript string method includes: if (document.title.includes('Test')).
This question already has answers here:
How can I use ranges in a switch case statement using JavaScript?
(7 answers)
Closed 2 years ago.
I'm building an app to check the expiration dates of items. The functionality of the app works but I want to be able to check if a string includes a sub-string in order to run my function which is a countdown timer. I haven't included much of the code but it should be sufficient. Code is Bellow
var userInput = prompt("What item would you like to look up?");
switch (userInput) {
case userInput.includes("cous cous"):
//run a setInterval function
case expression: resolves the expression and does an equality test of that value against the userInput.
Use an if() statement for this type of thing.
This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.
Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.
This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.
I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}
This question already has answers here:
How to get the name of an element with Javascript?
(5 answers)
Closed 9 years ago.
I have this code:
var parent = links[i].parentNode;
I'd like to write something like:
if (parent.typeOfElement == "div") {
...
}
How can I do that?
You can use .tagName, which is (for elements) the same as .nodeName.
So:
if (parent.tagName === "DIV") {
//
}
Note that the tag name is supposed to be returned in uppercase for HTML, but in XML (including xhtml) it is supposed to preserve the original case - which for xhtml means it should be lowercase. To be safe, and allow for any future changes to your document type and allow for non-standard browser behaviour you might want to convert to all upper or all lower:
if (parent.tagName.toUpperCase() === "DIV") {
//
}
In my experience .tagName is used much more often, but I gather that some consider .nodeName a better choice because it works for attributes (and more) as well as elements.
if (parent.nodeName == "div") {
...
}
See: http://www.javascriptkit.com/domref/elementproperties.shtml