Solved

Javascript number and length validation

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

910 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

23 Experts available now in Live!

Get 1:1 Help Now