Solved

Javascript number and length validation

Posted on 2014-04-04
8
415 Views
Last Modified: 2014-04-04
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

0
Comment
Question by:RickardP_GSI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39978587
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

0
 

Author Comment

by:RickardP_GSI
ID: 39978608
there is more to it - here is the URL:  http://uploads.guestservices.com/CASH_HANDLING/Cashier/index.htm
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39978633
there is more to it
Can you elaborate?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RickardP_GSI
ID: 39978644
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39978831
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

0
 

Author Comment

by:RickardP_GSI
ID: 39978854
It tells me I have a syntax error when I put your code in
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39978881
Sorry, too much C#  = )

Change line 3 to:

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

Open in new window

0
 

Author Comment

by:RickardP_GSI
ID: 39978925
ahhhhhhhhhhh PERFECT - thank you so much!!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question