Solved

Javascript number and length validation

Posted on 2014-04-04
8
409 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
there is more to it - here is the URL:  http://uploads.guestservices.com/CASH_HANDLING/Cashier/index.htm
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
there is more to it
Can you elaborate?
0
 

Author Comment

by:RickardP_GSI
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
It tells me I have a syntax error when I put your code in
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
ahhhhhhhhhhh PERFECT - thank you so much!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now