Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Need numeric validation for a field ina form (http://www.go4expert.com/forums/numeric-validation-field-ina-form-t28885/)

satyasampan 16Aug2012 11:49

Need numeric validation for a field ina form
 
:embarassei 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;}
}


shabbir 16Aug2012 11:56

Re: Need numeric validation for a field ina form
 
Your code should be doing fine unless you have some issues with other HTML.

satyasampan 16Aug2012 12:13

Re: Need numeric validation for a field ina form
 
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

shabbir 16Aug2012 12:15

Re: Need numeric validation for a field ina form
 
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

satyasampan 16Aug2012 13:01

Re: Need numeric validation for a field ina form
 
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>


shabbir 16Aug2012 14:05

Re: Need numeric validation for a field ina form
 
Show some effort of posting the problematic code and and not the complete code. No one would debug your complete issues.

lgco2012 17Aug2012 11:33

Re: Need numeric validation for a field ina form
 
Maybe you can share relevant code so I can reply easily to your question? Are you sure it is about the code?

sandysan9 17Aug2012 15:52

Re: Need numeric validation for a field ina form
 
Yes, i agree with you mas...when we knows the complete code then only can help in complete way.

ManzZup 15Sep2012 17:05

Re: Need numeric validation for a field ina form
 
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
}

?>


All times are GMT +5.5. The time now is 23:36.