Need numeric validation for a field ina form

satyasampan's Avatar, Join Date: Aug 2012
Newbie Member
i need to validate the form .. fields should not be empty is justified... but the last field i.e var z should be numeric ..please reply fast

Code:
function validateform()
  {
  var x=document.getElementById("nameid").value;
  if(x==''){alert("name should not be empty");return false;}
 var y=document.getElementById("qualificationid").value  ;
  if(y==''){alert("qualification should not be empty");return false;}
 var z=document.getElementById("rollnumid").value;
  if(z==''){alert("rollnum should not be empty");return false;}
}

Last edited by shabbir; 16Aug2012 at 11:55.. Reason: Code blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Your code should be doing fine unless you have some issues with other HTML.
0
satyasampan's Avatar, Join Date: Aug 2012
Newbie Member
i need to do numeric validation to last field wher if i enter an alphabet by default 0 is entring into table ...please help me with code
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Your issue may be with PHP Validation and not the code you have just posted. Try to share relevant code so that we can help
0
satyasampan's Avatar, Join Date: Aug 2012
Newbie Member
here isthe complete code ...please help me in validating rollnum field

PHP Code:
 <?php

$con 
mysql_connect("localhost","root","");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("tutorial"$con);

if(
$_REQUEST["submit"]=="submit")
{
$sql="INSERT INTO friends (name, qualification, rollnum)
VALUES
('
$_POST[name]','$_POST[qualification]','$_POST[rollnum]')";

if (!
mysql_query($sql,$con))
  {
  die(
'Error: ' mysql_error());
  }
}
mysql_close($con);
?> 





<html>
<head>
<script type="text/javascript">
function validateform()
  {
  valid=true;
  var Numbers = '0123456789';
  var x=document.getElementById("nameid").value;
  if(x==''){alert("name should not be empty");return false;}
 var y=document.getElementById("qualificationid").value;
  if(y==''){alert("qualification should not be empty");return false;}
 var z=document.getElementById("rollnumid").value;
 if(z==''){alert("rollnum should not be empty");return false;}
if(isNaN(z))
alert("Characters are not allowed in the Phone Number");
return false;
 
    }



</script>
<body>
<div style="position:absolute;top:100px;left:60px;">
<h1 align="center"><font size=5 color="red">submit details to the table</font></h1> 
 <form name=form action="form.php" method="post">
<table  border=2 cellspacing=0 cellpadding=0>
<tr><td>name</td><td><input type="text" name="name" id="nameid" /></td></tr></br>
<tr><td>qualification</td><td> <input type="text" name="qualification" id="qualificationid" /></td></tr></br>
<tr><td>rollnum</td><td><input type="text" name="rollnum" id="rollnumid"/></td></tr></br>
</table></br>
<input type="submit" name="submit" value="submit" onclick="return validateform()">
</form></div>



<div style="position:absolute;bottom:60px;left:60px;">
<?php
echo "the added record is $_REQUEST[name],$_REQUEST[qualification],$_REQUEST[rollnum] ";
?></div>



<div style="position:absolute;top:60px;left:600px;">
<html>
<head>friends table</head>
<title>db</title>
<body><br>
<?php
$con 
mysql_connect("localhost","root","");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }
echo 
"the table from the database is as follows";

$db_selected mysql_select_db("tutorial",$con);
$string="select * from friends";
$result=mysql_query($string,$con);

?>


<table align="center" border="4" cellpadding="5" cellspacing="5" width="50%">
<tr>
<th>name</th>
<th>qualification</th>
<th>rollnum</th>
</tr>
<?php

while($row=mysql_fetch_array($result))
{
  echo 
"<tr><td align='center'>$row[name]</td><td align='center'>$row[qualification]</td><td align='center'>$row[rollnum]</td></tr>";
}
?>
</table></div>
</body>
</html>
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Show some effort of posting the problematic code and and not the complete code. No one would debug your complete issues.
sandysan9 like this
0
lgco2012's Avatar
Banned
Maybe you can share relevant code so I can reply easily to your question? Are you sure it is about the code?
sandysan9 like this
0
sandysan9's Avatar, Join Date: Aug 2012
Light Poster
Yes, i agree with you mas...when we knows the complete code then only can help in complete way.
0
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
i dont get your issue very well
but from what i got you need to make sure that the last field, that is 'rollnumid' is always a numeric value

JS level check

use this function to check whether it is a number

<script>
Code:
function isNumeric(n){
    if(!isNaN(n) && typeof(n)=='number'){
        return true;
    }else{
       return false;
    }
}

</script>
PHP level check
simply use the is_numeric method

PHP Code:
<?php
if(is_numeric($rollid)){
 do 
stuff
}
?>