#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    JavaScript issue


    Hi dear,,

    this is my first post in this forum,

    I am facing an issue with the last if statement, it is being ignored ,

    your kind help is highly appreciated.

    <script type="text/javascript">
    function ValidateMobNumber(mobno) {
    var fld = document.getElementById(mobno);

    if (fld.value == "") {
    alert("You didn't enter a phone number.");
    fld.value = "";
    fld.focus();
    return false;
    }
    if (isNaN(fld.value)) {
    alert("The phone number contains illegal characters.");
    fld.value = "";
    fld.focus();
    return false;
    }
    if (!(fld.value.length == 8)) {
    alert("The phone number is the wrong length. \nPlease enter 8 digit mobile no.");
    fld.value = "";
    fld.focus();
    return false;
    }
    if (fld.charAt(0)!="9"){
    alert("Cell no should start with 9");
    fld.value = "";
    fld.focus();
    return false;
    }

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

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    873
    Rep Power
    275
    if you are looking for an actual number, I believe you need to do something like:
    Code:
    if(fld.charAt(0)!==9){
    //run your code.....
    }
    your code turns 9 into a string because you surrounded it with dbl quotes.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by DonR
    if you are looking for an actual number, I believe you need to do something like:
    Code:
    if(fld.charAt(0)!==9){
    //run your code.....
    }
    your code turns 9 into a string because you surrounded it with dbl quotes.

    thank you for your quick response.

    I've tried what you said but it is still having the same issue.

    below is the complete code :
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <script type="text/javascript">
    function ValidateMobNumber(mobno) {
    var fld = document.getElementById(mobno);

    if (fld.value == "") {
    alert("You didn't enter a phone number.");
    // fld.value = "";
    // fld.focus();
    return false;
    }
    if (isNaN(fld.value)) {
    alert("The phone number contains illegal characters.");
    // fld.value = "";
    // fld.focus();
    return false;
    }
    if (!(fld.value.length == 8)) {
    alert("The phone number is the wrong length. \nPlease enter 8 digit mobile no.");
    // fld.value = "";
    // fld.focus();
    return false;
    }
    if(fld.charAt(0)!=9){
    alert("Cell no should start with 9");
    // fld.value = "";
    // fld.focus();
    return false;
    }
    }
    </script>


    <body>
    <?php include 'conn.php';
    session_start();
    $va = $_POST['acc_no'];
    $vr = $_POST['acc_no'];
    $pr = $_POST['acc_preffix'];
    $result =mysql_query("SELECT * FROM customer_db where cust_account_no='$va' and acc_preffix = '$pr'");
    ?>

    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <form name="mobvalidate" method="post" action="update.php">
    <td>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td colspan="4"><strong>Account No : <?php echo $pr,$va = $_POST['acc_no'];?> </strong> </td>
    </tr>

    <tr>
    <td align="center"><strong>Username</strong></td>
    <td align="center"><strong>Mobile</strong></td>

    </tr>

    <?php
    while($row = mysql_fetch_array($result))
    { ?>

    <tr>
    <td><?php echo $row['cust_name']; ?></td>
    <td><input name="name" type="number" id="mobno" value="<?php echo $row['cust_mobile_no']; ?>" maxlength="8" ></td>
    <td><input name="ana" type="hidden" id="name" value="<?php echo $row['cust_account_no']; ?>"></td>

    <td align="center"><input type="submit" name="Submit" value="Save" onclick="return ValidateMobNumber('mobno')"></td>
    </tr>
    <tr>
    </tr>
    <?php
    }
    ?>
    </table>
    </td>

    </table>

    </body>
    </html>
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,945
    Rep Power
    4033
    You should be using fld.value.charAt(0). You need to access the value before you try and get it's first character.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by kicken
    You should be using fld.value.charAt(0). You need to access the value before you try and get it's first character.
    thank you very much,, it working 100% ..




    thank you all

IMN logo majestic logo threadwatch logo seochat tools logo