Link to home
Start Free TrialLog in
Avatar of RickardP_GSI
RickardP_GSIFlag for United States of America

asked on

Javascript number and length validation

This is validation for the length of the input number but i need to validate that the actual input is in fact a number with no letters

  if (document.login.userID.value == "" || document.login.userID.value.length != 6) {
    alert("Please enter your 6-digit Employee ID to begin your training.")
    document.login.userID.focus();
    return false;
  }

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

You could use a regular expression and take care of it all in one shot:

e.g.

RegExp reg = new RegExp("^[0-9]{6}$");

if (!reg.test(document.login.userID.value)) {
    alert("Please enter your 6-digit Employee ID to begin your training.")
    document.login.userID.focus();
    return false;
}

Open in new window


The pattern means:

^      - Start of string
[0-9]  - Any numeric digit
{6}    - 6 of the thing to the left; in this case, any numeric digit
$      - End of string

Open in new window

Avatar of RickardP_GSI

ASKER

there is more to it - here is the URL:  http://uploads.guestservices.com/CASH_HANDLING/Cashier/index.htm
there is more to it
Can you elaborate?
I mean there is more javascript in the file.  If you look at line 103 you will see where it starts -  http://uploads.guestservices.com/CASH_HANDLING/Cashier/index.htm
I must be missing something, but I don't see an issue. Just include what I suggest above as the first if clause.

e.g.

function saveLoginName() {

    RegExp reg = new RegExp("^[0-9]{6}$");

    if (!reg.test(document.login.userID.value)) {
        alert("Please enter your 6-digit Employee ID to begin your training.")
        document.login.userID.focus();
        return false;
    } else if(document.login.userFirst.value == "" || document.login.userFirst.value == "") {
        alert("Your full first name must be entered");
        document.login.userFirst.focus()
        return false
    } else if(document.login.userLast.value == "" || document.login.userLast.value == "") {
        alert("Your last name must be entered");
        document.login.userLast.focus()
        return false
    }

    setCookie("userName", document.login.userID.value)
    setCookie("userFirst", document.login.userFirst.value)
    setCookie("userLast", document.login.userLast.value)
    setCookie("studn", document.login.userID.value)
    document.login.submit()
}

Open in new window

It tells me I have a syntax error when I put your code in
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahhhhhhhhhhh PERFECT - thank you so much!!