简体   繁体   English

如何仅在此特定脚本中验证字母-javascript

[英]How to validate for letters only in this specific script - javascript

I would need a little help of you today, i'm wondering how can i validate my script for letters only on first 2 inputs? 我今天需要您的帮助,我想知道如何才能仅对前2个输入验证字母的脚本?

I'm using javascript to validate my form below: I have managed to validate for email though and zip (numbers only)... 我正在使用javascript验证以下表单:尽管设法验证了电子邮件和zip(仅数字)...

<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
<!--
function checkForEmail()
{

   var emailID = document.myForm.EMail.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Alert")
       document.myForm.EMail.focus() ;
       return false;
   }
   return( true );
}

function validate()
{
   if( document.myForm.Name.value == "" )
   {
     alert( "Alert" );
     document.myForm.Name.focus() ;
     return false;
   }
   if( document.myForm.Surname.value == "" )
   {
     alert( "Alert" );
     document.myForm.Surname.focus() ;
     return false;
   }
   if( document.myForm.EMail.value == "" )
   {
     alert( "Alert" );
     document.myForm.EMail.focus() ;
     return false;
   }else{
     var ret = checkForEmail();
     if( ret == false )
     {
          return false;
     }
   }
   if( document.myForm.zip.value == "" ||
           isNaN( document.myForm.zip.value ) ||
           document.myForm.zip.value.length != 13 )
   {
     alert( "Alert" );
     document.myForm.zip.focus() ;
     return false;
   }
   return( true );
}
//-->
</script>
</head>
<body>
 <form action="/cgi-bin/test.cgi" name="myForm"  onsubmit="return(validate());">
 <table cellspacing="2" cellpadding="2" border="1">
 <tr>
   <td align="right">Name</td>
   <td><input type="text" name="Name" /></td>
 </tr>
  <tr>
   <td align="right">Surname</td>
   <td><input type="text" name="Surname" /></td>
 </tr>
 <tr>
   <td align="right">EMail</td>
   <td><input type="text" name="EMail" /></td>
 </tr>
 <tr>
   <td align="right">zip</td>
   <td><input type="text" name="zip" /></td>
 </tr>
 <tr>
 <td align="right"></td>
 <td><input type="submit" value="Submit" /></td>
 </tr>
 </table>
 </form>
 </body>
 </html>

how can i validate my script for letters only on first 2 inputs 我如何才能仅在前2个输入上验证我的脚本是否包含字母

// Check s contains at least one letter and is only letters
function checkLettersOnly(s) {
  return /^[a-z]+$/i.test(s);
}

if (!checkLettersOnly(document.myForm.Name.value)) {
  // not letters only
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM