#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0

    Javascript Validation


    Hi, was just wondering if anyone knew any javascript to validate a input to a textbox to make sure it's numerical entries only. Also I can't call the function from the onsubmit action as i already have other validation in the form. It looks like this so far, just to make it clearer.

    <script language="JavaScript">

    function validRequired(formField,fieldLabel)
    {
    var result = true;

    if (formField.value == "")
    {
    alert('Please enter the number of shares you wish to buy');
    formField.focus();
    result = false;
    }

    return result;
    }

    function check_length(user_input) {
    if ((user_input.length < 1) || (user_input.length > 5)) {
    alert("Please ensure your figure is between 1 and 5 characters long.");
    formField.focus();
    result = false;
    }
    }

    function validateForm(buy)
    {
    // Customize these calls for your form

    // Start ------->
    if (!validRequired(buy.NoOfShares,"No of Shares"))
    return false;

    if (!check_length(buy.NoOfShares.value))
    return false;

    return true;
    }


    //-->
    </script>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    Well - you've already solved the problem of calling multiple validators from Form.onsubmit, the clever way: calling a 'master' function, and then calling sub-validators from there. It looks as if everything you posted is checking the same field, however; why not roll them up together into one?

    <script type="text/javascript" language="JavaScript">

    function check_NoOfShares(field) {
    var msg ='', v = field.value;
    if (isNaN(v) || v.indexOf('.') != -1) msg = 'Please enter a valid whole number here.';
    if (v.length>5) msg = 'Please ensure your figure is between 1 and 5 characters long.';
    if (!v || /^\s+$/.test(v)) msg = 'Please enter the number of shares you wish to buy.';
    if (msg) {
    alert(msg);
    field.focus();
    field.select();
    return false;
    }
    return true;
    }

    function validateForm(f) {
    if (!check_NoOfShares(f.NoOfShares)) return false;
    return true;
    }

    </script>

    ...and:

    <form onsubmit="return validateForm(this)">

    As above, you don't need the 'master' validator at all; but I'm assuming you'll be doing other checks. The last check (with the RegExp) checks for an empty field or all spaces.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0
    Thanks a million
  6. #4
  7. superficial
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Peterborough, England
    Posts
    188
    Rep Power
    13
    additional - if you want a decent resource, i recommend this

    _______________
    Matt

IMN logo majestic logo threadwatch logo seochat tools logo