/    Sign up×
Community /Pin to ProfileBookmark

Validating a length property

? I am writing a validation script for a small order form. I need to verify that the number is 5 digits long. The script below works except it does not recognize the length validation. I have tried (IDLen != 5) but that does just the opposite and won’t vaildate any length. Thanks for any help!

function validate(){

var IDLen = document.frmCustOrders.txtOrderID.length;
var ordID = parseInt(document.frmCustOrders.txtOrderID.value);

if (isNaN(ordID) == true) {
alert(“Order ID must be a number”);
document.frmCustOrders.txtOrderID.select();
return false;

}else if ((ordID < 1) || (ordID > 99999)) {
alert(“Order ID must be a number between 1 & 99,999”);
document.frmCustOrders.txtOrderID.select();
return false;

}else if (IDLen < 5) {
alert(“Order ID must be a 5 Digit number between 1 & 99,999”);
document.frmCustOrders.txtOrderID.select();
return false;
}else{
return true;
alert(“Order ID Good”);
}

Nasus

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoNov 12.2005 — Few things:

  • 1. Instead of checking for NaN, I would just add:


  • onkeyup="this.value=this.value.replace(/D/,'')"

    to all of your inputs. That will allow only numbers to be inputed.

  • 2. Then make your function look like this:
    <i>
    </i>
    function validate()
    {
    var IDLen = document.frmCustOrders.txtOrderID.length;
    var ordID = parseInt(document.frmCustOrders.txtOrderID.value);

    if ((ordID &lt; 1) || (ordID &gt; 99999)) {
    alert("Order ID must be a number between 1 &amp; 99,999");
    document.frmCustOrders.txtOrderID.select();
    return false;
    }

    else if (IDLen &gt; 5)
    {
    alert("Order ID must be a 5 Digit number between 1 &amp; 99,999");
    document.frmCustOrders.txtOrderID.select();
    return false;
    }

    else
    {
    return true;
    alert("Order ID Good");
    }
    }
  • Copy linkTweet thisAlerts:
    @kinkNov 13.2005 — maybe this will help this o0ne works

    <script type="text/javascript">

    function afunction()

    {

    var nm = document.ti.nm.value;

    var nb = nm.length;

    if(nb > 0)bfunction();

    else

    alert("Please enter a first name");

    return(false);


    }

    </script>
    Copy linkTweet thisAlerts:
    @NasusauthorNov 13.2005 — The method of putting the value into a variable then using the length property worked great - Thanks for the help!!!!
    ×

    Success!

    Help @Nasus spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...