Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript number and length validation

Posted on 2014-04-04
8
Medium Priority
?
447 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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

877 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