50 users online (1 members and 49 guests)  


  Results 1 to 2 of 2

Related

  1. Dropdown menu hides behind dropdown box    Forum: Javascript Forum
    Replies: 0
  2. Help with Javascript form validation please!!    Forum: Javascript Forum
    Replies: 2
  1. #1
    jduk's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2004
    Posts
    1

    Javascript Validation - Dropdown Box

    Hi friends,

    Here is a JavaScript validation problem. If user selects the option 'solo' or 'switch' card from drop down and if he do not enter his 'issue number', an alert should say a message, "Please enter your Issue Number".

    I have some problem in the code, can some body help me in this regard please.

    --------------------------------------

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>::: try - issue number :::</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    function check() {
    var accepted=null;

    if (form.name_on_card.value.length == 0) {
    alert("Please enter the name on card.");
    form.name_on_card.focus();
    return false;
    }
    if (form.card_number.value.length == 0) {
    alert("Please enter a card number.");
    form.card_number.focus();
    return false;
    }
    if ((form.card_type.options[form.card_type.selectedIndex].value == solo) && (form.issue_number.value.length == 0)) {
    alert("You have selected Solo card, please enter Issue Number" );
    form.issue_number.focus();
    return false;
    }
    if (form.cvv2.value.length == 0) {
    alert("Please enter a CVV2 number.");
    form.cvv2.focus();
    return false;
    }
    }
    //-->
    </script>
    </head>
    <body>
    <form name="form" method="post" action="xyz.html">
    <table border="0" cellpadding="1" cellspacing="0">
    <tr>
    <td align="right" valign="top">Name on card</td>
    <td valign="top"><input name="name_on_card" type="text" size="35" maxlength="35">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Card number:</td>
    <td valign="top"><input name="card_number" type="text" size="18" maxlength="22">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Card type</td>
    <td valign="top"><select name="card_type">
    <option value="--" selected>Select a credit card</option>
    <option value="visa">Visa</option>
    <option value="master">Master</option>
    <option value="amex">American Express</option>
    <option value="switch">Switch</option>
    <option value="solo">Solo</option>
    </select>
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Issue Number</td>
    <td valign="top"><input name="issue_number" type="text" size="5" maxlength="2">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Security code</td>
    <td valign="top"><input name="security_code" type="text" id="security_code" size="5" maxlength="5">
    </td>
    </tr>
    </table>
    <p align="left">
    <input name="submit" type="submit" id="submit" onClick="return check()" value="Submit">
    <input name="reset" type="reset" value="Reset">
    </form>
    </body>
    </html>

    --------------------------------------

    Thanks for oyur time....

  2. #2
    krak_d's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Liverpool
    Posts
    166
    Quote Originally Posted by jduk
    Hi friends,

    Here is a JavaScript validation problem. If user selects the option 'solo' or 'switch' card from drop down and if he do not enter his 'issue number', an alert should say a message, "Please enter your Issue Number".

    I have some problem in the code, can some body help me in this regard please.

    --------------------------------------

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>::: try - issue number :::</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    function check() {
    var accepted=null;

    if (form.name_on_card.value.length == 0) {
    alert("Please enter the name on card.");
    form.name_on_card.focus();
    return false;
    }
    if (form.card_number.value.length == 0) {
    alert("Please enter a card number.");
    form.card_number.focus();
    return false;
    }
    if ((form.card_type.options[form.card_type.selectedIndex].value == solo) && (form.issue_number.value.length == 0)) {
    alert("You have selected Solo card, please enter Issue Number" );
    form.issue_number.focus();
    return false;
    }
    if (form.cvv2.value.length == 0) {
    alert("Please enter a CVV2 number.");
    form.cvv2.focus();
    return false;
    }
    }
    //-->
    </script>
    </head>
    <body>
    <form name="form" method="post" action="xyz.html">
    <table border="0" cellpadding="1" cellspacing="0">
    <tr>
    <td align="right" valign="top">Name on card</td>
    <td valign="top"><input name="name_on_card" type="text" size="35" maxlength="35">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Card number:</td>
    <td valign="top"><input name="card_number" type="text" size="18" maxlength="22">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Card type</td>
    <td valign="top"><select name="card_type">
    <option value="--" selected>Select a credit card</option>
    <option value="visa">Visa</option>
    <option value="master">Master</option>
    <option value="amex">American Express</option>
    <option value="switch">Switch</option>
    <option value="solo">Solo</option>
    </select>
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Issue Number</td>
    <td valign="top"><input name="issue_number" type="text" size="5" maxlength="2">
    </td>
    </tr>
    <tr>
    <td align="right" valign="top">Security code</td>
    <td valign="top"><input name="security_code" type="text" id="security_code" size="5" maxlength="5">
    </td>
    </tr>
    </table>
    <p align="left">
    <input name="submit" type="submit" id="submit" onClick="return check()" value="Submit">
    <input name="reset" type="reset" value="Reset">
    </form>
    </body>
    </html>

    --------------------------------------

    Thanks for oyur time....
    Hi,

    i think from what you are saying you need to validate a field for 'nothing being' entered or 'all spaces'.

    You will need to create a function to do this then call for the function in your 'if' statement!

    eg.
    function isBlank(testStr)
    {
    if (testStr.length == 0) // nothing entered?
    return true
    for (var i = 0; i <= testStr.length-1; i++) // all spaces?
    if (testStr.charAt(i) != " ")
    return false
    return true
    }

    ....and then call for the function!

    function validateissue_number(issue_number)
    {
    if (isBlank(issue_number)) // issue number field blank?
    {
    alert("Enter your issue number, please!")
    return false
    }


    My apologies if this isnt what you're looking for, JavaScript isn't a favourite of mine!



Tags for this Thread