0

The objective of this code is to check the name the user inputs. If the value contains something other than -abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ the function will throw an error.

I am unable to get this to work, and I'm not allowed to use Regular expressions. I've also tried String1.indexOf(usr.substr(i,1)) > -1) but that doesn't seem to work neither.

function nameValidation(username) {
    var usr = document.getElementById("username").value;
    usr = usr.trim();
    var alpha = "-abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var ok = 1;
    for (var i = 0; i < usr.length; i++) {
        if (!isNaN(usr[i])) {
            ok = 0;
            break;
        } else {
            ok = 1;
            document.getElementById("fnerror").innerHTML = "";
            document.getElementById("username").style.borderColor = "lightgrey";
            return true;
        }
    }

    if (ok == 0) {
        document.getElementById("fnerror").innerHTML = "X Enter Upper and lower case      letters, hypen, apostrohe only please";
        return false;
    }
    return true;
}
8
  • 2
    Can't you just match against /[^azAZ-']/? Commented Aug 4, 2015 at 21:08
  • I forgot to mention I'm not allowed to use regEx. Ill add to the post now Commented Aug 4, 2015 at 21:10
  • 2
    You're... not allowed to use regex? Commented Aug 4, 2015 at 21:12
  • 4
    Homework question, I assume. Commented Aug 4, 2015 at 21:12
  • Well, if you could use Regex, this would be a one-liner. Something like /[^A-z-']/ Commented Aug 4, 2015 at 21:13

5 Answers 5

3

Something like this, maybe:

function isValidUsername(username) {
  var alpha = "-'ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  username = username.toUpperCase();
  for (var i = 0, l = username.length; i < l; i++) {
    if (alpha.indexOf(username[i]) === -1) return false;
  }
  return true;
}

Cheaper to upper-case the string and therefore have a shorter set of characters to test against (though probably marginal at best because there's a cost to even native-uppercasing..)

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

1 Comment

Darn, beat me to it. Although you can declare alpha outside of the function, that way you're not unnecessarily declaring it each call (since it will always be the same, not dynamic to the function).
3

You can do it in a more "functional way", by using every method, which allow us to break a loop instead of foreach.

The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value (a value that becomes false when converted to a Boolean). If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

Array.proototype.every

So :

function check(){

  //Get value input and transform it into array
  var value = document.querySelector('#username').value.split('');

  var alpha = "-abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  //If there is an error, stop the loop and return result
  return value.every(function(elm){

    //check if elm is an alpha string
    return alpha.indexOf(elm) > -1;
  });

}

Comments

2

The easiest (to understand at least) solution (that doesn't use regex), would be to loop through your string character by character and check .indexOf against your list of allowed characters, something like:

for (var i = 0; i < input.length; i++) {
    if (alpha.indexOf(input[i])==-1) {
        console.log("ERROR");
        break;
    }
}

Comments

0

EDITED: I read the question wrong and thought you wanted to return true if there's a letter. It will now make sure that each character is within the ASCII values of A and z.

text = "ABCDEFzzxasd1";
valid = true;

for( i = 0; i < text.length; i++ ) {
    if ( text.charCodeAt(i) < 65 || text.charCodeAt(i) > 122 ) {
        alert("Woah, that's not a letter!");
        valid = false;
        break;
    }
}

1 Comment

Thank you! this helped me as well as the other comments!
0

Begin with

var alpha = "-abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');

function duplicates( element, index, array ){
  return array.indexOf(element) == index;
}

function isNameFormatCorrect( userName ){
  return ( alpha.concat(username.split(''))
    .filter( duplicates ).length === alpha.length );
}

Then

var username = "YourUserNameHere-"
isNameFormatCorrect( username ); => true;

var username = "YourUserNameHere-**"
isNameFormatCorrect( username ); => false;

Comments

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.