Javascript表单验证(小数位)

时间:2014-02-07 10:32:35

标签: javascript

我使用以下脚本来验证表单,并希望能够确保输入器在最后一个字段(金额)上正确输入带小数位的数值。

任何人都可以帮忙吗?

<script type="text/javascript">
<!--
function validate()
{
   if( document.create_invoice.job_no.value == "" )
   {
     alert( "Please provide a Job No" );
     document.create_invoice.job_no.focus() ;
     return false;
   }
   if( document.create_invoice.contact.value == "-1" )
   {
     alert( "Please provide a Customer Contact" );
     return false;
   }
   if( document.create_invoice.employee.value == "-1" )
   {
     alert( "Please provide an Employee" );
     return false;
   }
   if( document.create_invoice.invoice_no.value == "" )
   {
     alert( "Please provide an Invoice No" );
     document.create_invoice.invoice_no.focus() ;
     return false;
   }
   if( document.create_invoice.address.value == "" )
   {
     alert( "Please provide an Address" );
     document.create_invoice.address.focus() ;
     return false;
   }
   if( document.create_invoice.amount.value == "" )
   {
     alert( "Please provide an Amount" );
     document.create_invoice.amount.focus() ;
     return false;
   }
}
//-->
</script>

谢谢,

约翰

1 个答案:

答案 0 :(得分:2)

您可能想要使用parseFloat()...尝试:

if( parseFloat(document.create_invoice.amount.value) != document.create_invoice.amount.value )
{
   alert( "Please provide a decimal value" );
   document.create_invoice.amount.focus() ;
   return false;
}

这可能只是为你做的伎俩。