Tuesday, November 10, 2009

Decimal Number Validation in javascript?

How to validate decimal number using JavaScript?


You can validate decimal number, floating point number using this javascript. If input number is not in the correct format it shows an error message.


Here is the javascript and the html code. Try to validate floating point number using this piece of code


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.table_text{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
</style>
<script type="text/javascript">
function decimal_check() {
var str=document.forms["decimal_form"].txtDecimalValue.value; 
if(str!=""){
var regDeci=/^[-+]?\d+(\.\d+)?$/;
if(regDeci.test(str)==true){
alert(str+" Is Valid decimal number");
return true;
}else{
alert("Please enter valid number");
return false;
}
}else{
alert("Please enter valid number");
return false;
}
}
</script>
</head>


<body>
<form id="decimal_form" name="decimal_form" method="post" action="" onsubmit="return decimal_check();">
  <table width="200" border="0" class="table_text">
    <tr>
      <td>Decimal Value </td>
      <td><label>
        <input name="txtDecimalValue" type="text" id="txtDecimalValue" />
      </label></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input name="btn_submit" type="submit" id="btn_submit" value="Submit" />
      </label></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

1 comments:

Anonymous said...

This is the third time I've been to your website. Thank you for explaining more details.
My webpage - Remove Decals