脚本中的每个文本框验证

时间:2014-10-27 09:07:58

标签: javascript

这是我的脚本,我想在字段为空时创建警报,然后在按下回车键时在屏幕上显示警报。我想要的脚本包括我在文本框中写入时按Enter键然后验证该关键字并转到下一个文本框。

<script type="text/javascript">
function validate()
{
var letters = /^[A-Za-z]+$/;
 if( document.myForm.vname.value == "" )
   {
     alert( "Please provide your Vehicle Name!" );
     document.myForm.vname.focus();
     return false;
   }
else if (document.myForm.vname.value.length > 15)
    {
        alert("Vehicle name cannot be more than 15 characters");
        document.myForm.vname.focus() ;
        return false;
    }
else if(!document.myForm.vname.value.match(letters))
  {
  alert("Enter Only Characters ");
  document.myForm.vname.focus() ;
        return false;
    }
 if( document.myForm.usage.value == "" )
   {
    alert( "Please provide your Vehicle Usage!" );
     document.myForm.usage.focus() ;
     return false;
   }
 if( document.myForm.vtype.value == 0 )
   {
     alert( "Please provide your Vehicle Type!" );
     document.myForm.vtype.focus() ;
     return false;
   }
 if( document.myForm.vmodelno.value == "" )
   {
     alert( "Please provide your Model No!" );
     document.myForm.vmodelno.focus() ;
     return false;
   }
if( document.myForm.ftype.value == 0 )
   {
     alert( "Please provide your Fuel Type!" );
      document.myForm.ftype.focus() ;
     return false;
   }
if( document.myForm.cmp.value == "" )
   {
     alert( "Please provide your Company Name!" );
     document.myForm.cmp.focus() ;
     return false;
   }
return( true );
}
</script>

1 个答案:

答案 0 :(得分:0)

按下输入表格即可提交。

相反,您可以在焦点超出每个输入字段时调用。像:

$("#myfield").blur(function(){
     ----
});

此代码适用于Jquery