您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關JavaScript如何驗證表單空值及郵箱格式,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
運行效果圖如下:
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="zh-cn" /> <title>Javascript 表單驗證</title> <body> <h4>(一)驗證必填項是否有空值。</h4> <form action = "submitpage.html" onsubmit = "return validate_form(this)" method = "post"> Name:<input type = "text" name = "name" size = "20"> <input type = "submit" value = "Submit"> </form> <h4>(二)驗證Email格式是否正確。</h4> <form action = "submitpage.html" onsubmit = "return is_email_form(this)" method = "post"> Email:<input type = "text" name = "email" size = "20"> <input type = "submit" value = "OK"> </form> <script> //判斷內容是否為空 function validate_form(thisform){ with (thisform){ if (!validate_required(name,"Name must be filled out!")){ name.focus(); return false } } } function validate_required(field,alerttxt){ with (field){ if (value==null||value==""){ alert(alerttxt); return false }else { return true } } } //判斷內容是否符合email的格式 function is_email_form(thisform){ with(thisform){ if(!checkEmail(email,"Not a valid e-mail address!")){ email.focus(); return false; } } } function checkEmail(field, alertText){ with(field){ apos = value.indexOf("@"); dotPos = value.indexOf("."); if(apos<1 || dotPos-apos<2){ alert(alertText); return false; }else{ return true; } } } </script> </body> </html>
關于“JavaScript如何驗證表單空值及郵箱格式”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。