1

This code wants to check an array if the name is in it or not, and that the first letter of all the names in the array is uppercase. There are many users who write their name without using an uppercase letter in their first name.

So I want to pass this condition in the code, by Regular Expression ( /i ), I can use this with a string and some other functions, but I can't use it with a variable.

Can anybody help me?

function runTest() {
    "use strict";
    
    var value = document.getElementById("inin").value;
    
    if (names.indexOf(value) > -1) {
        x.innerHTML = "yes " + value + " your name is here, your are fully approved";
    } else {
        x.innerHTML = "Sorry, Your name isn't here";
    }
}

1
  • Make sure you list is all lower or upper case and just transform the incoming value to that case. Commented Nov 30, 2016 at 1:21

1 Answer 1

1

You do not need a regular expression in this case. Just make sure that your list is all lower or upper case and just transform the incoming value to that case.

var outputEl = document.getElementById("output");
var names = [ 'mary', 'bob', 'joseph' ];

function runTest() {
  "use strict";

  var value = document.getElementById("inin").value;

  if (names.indexOf(value.toLowerCase()) > -1) {
    outputEl.innerHTML = "Yes " + value + ", your name is here, you're fully approved.";
  } else {
    outputEl.innerHTML = "Sorry, Your name isn't here.";
  }
}
<input type="text" id="inin" value="Bob" />
<input type="button" value="Test" onClick="runTest()" />
<br />
<span id="output"></span>

Sign up to request clarification or add additional context in comments.

2 Comments

And, if i want to search on Big string that contains upper and lower case ?! I can't use this way, what will i do ??
Example: Your list of names should be transformed to lowercase, initially. Then just transform your input name to lowercase. You will only be dealing with lowercase. There should not be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.