SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript validation for numerical input to textbox

    Hi, was just wondering if anyone knew any javascript to validate 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 fuctions 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
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never read your code, but this function does exactly what your asking for

    PHP Code:
        Function isInt(textObj
        { 
            var 
    newValue textObj.value
            var 
    newLength newValue.length
            
            For(var 
    0!= newLengthi++) 
            { 
                
    aChar newValue.substring(i,i+1); 
            
                If(
    aChar "0" || aChar "9"
                { 
                    Return 
    False
                } 
            } 
            
            Return 
    True
        } 

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use isNaN (is-Not-a-Number) to check the field.
    if(isNaN(document.form_name.field_name.value))
    {
    do something
    }

    If the form field has anything else besides numbers then the if will be true.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •