September 9th, 2013, 02:10 AM
-
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>
September 9th, 2013, 02:23 AM
-
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.
September 9th, 2013, 03:03 AM
-
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>
September 9th, 2013, 03:19 AM
-
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
If I helped you out, show some love with some reputation, or tip with Bitcoins to
1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
September 9th, 2013, 03:27 AM
-
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