1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need numeric validation for a field ina form

Discussion in 'Web Design, HTML And CSS' started by satyasampan, Aug 16, 2012.

  1. satyasampan

    satyasampan New Member

    Joined:
    Aug 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    :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;}
    }
     
    Last edited by a moderator: Aug 16, 2012
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Your code should be doing fine unless you have some issues with other HTML.
     
  3. satyasampan

    satyasampan New Member

    Joined:
    Aug 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    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
     
  5. satyasampan

    satyasampan New Member

    Joined:
    Aug 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    here isthe complete code ...please help me in validating rollnum field

    PHP:
     <?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>
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Show some effort of posting the problematic code and and not the complete code. No one would debug your complete issues.
     
    sandysan9 likes this.
  7. lgco2012

    lgco2012 Banned

    Joined:
    May 23, 2012
    Messages:
    6
    Likes Received:
    1
    Trophy Points:
    0
    Maybe you can share relevant code so I can reply easily to your question? Are you sure it is about the code?
     
    sandysan9 likes this.
  8. sandysan9

    sandysan9 New Member

    Joined:
    Aug 3, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Yes, i agree with you mas...when we knows the complete code then only can help in complete way.
     
  9. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    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:
    <?php
    if(is_numeric($rollid)){
     do 
    stuff
    }
    ?>
     

Share This Page