We help IT Professionals succeed at work.

Validating a username input field - similar to coldfusion LEFT function

Medium Priority
Last Modified: 2013-12-16
I have this:

function validateForm() {
      var valid = true;
      if (document.LoginForm.ContactNo.value.length == 0)      {
            alert("Please Enter a Username.");
            valid = false;

Which checks if no user name has been entered (if true show alert box)

Also this:

      else if (document.LoginForm.ContactNo.value != "ADMN") {
            alert("Please enter an ADMN number.");
            valid = false;
      else {
            return valid;

Which checks if the entered consists exactly of the letters ADMN

However i need a case which checks if the first FOUR letters only of the entered text consists of the letters ADMN not that the whole string is just ADMN.

I believe in Coldfusion the syntax would be something like: Left(string, 4) EQ "ADMN".

I just cannot work out how to do this in Javascript.

Watch Question

if(document.LoginForm.ContactNo.value.substring(0,4) == "ADMN")

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.