Calling functions with button

Discussion in 'JavaScript' started by mcdeere02, Aug 7, 2009.

Thread Status:
Not open for further replies.
  1. #1
    The code below is suppossed to take an input in a textbox and upon the click of a button call a function to check the input.
    I cannot get this to work at all the text box and button appear but the code for the function appears on the page and the button press does nothing.
    Any advice here please :confused:

    // Creates text box for bank sort code to be entered
    <p>The layout used in this input is three pairs of numbers seperated by -. Thanks for taking the time to read this!</p>

    <input type="text" name="Bank_Sort_Code" maxlength="8" />



    <p><input type="button" value="Click here" onClick="validateBankNo();"></p>


    <script language="javascript" type="text/javascript">
    function validateBankNo()
    {
    if(ereg('^[0-9]{2}-[0-9]{2}-[0-9]{2}$', $Bank_Sort_Code))
    return True;
    else
    return false;

    }
    </script>
     
    mcdeere02, Aug 7, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what's? what's $Bank_Sort_Code?
     
    JavaScriptBank.com, Aug 7, 2009 IP
  3. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am not sure what is $Bank_Sort_Code... Are you atring to write any php code inside the javascript or are you using any json or jquery like libraries ?

    If not here is one way you can do this

    
    <script language="javascript" type="text/javascript">
    function validateBankNo()
    {
    
        var Bank_Sort_Code = document.getElementById("Bank_Sort_Code");
    
        if(ereg('^[0-9]{2}-[0-9]{2}-[0-9]{2}$', Bank_Sort_Code))  
            alert("Okay"); return True;
        else
           return false;
    }
    </script>  
    
    <input type="text" id="Bank_Sort_Code" name="Bank_Sort_Code" maxlength="8">
    
    <p><input type="button" value="Click here" onClick="validateBankNo();"></p>
    
    Code (markup):
     
    Last edited: Aug 9, 2009
    HivelocityDD, Aug 9, 2009 IP
Thread Status:
Not open for further replies.