簡體   English   中英

如何僅在此特定腳本中驗證字母-javascript

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

我今天需要您的幫助,我想知道如何才能僅對前2個輸入驗證字母的腳本?

我正在使用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>

我如何才能僅在前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