簡體   English   中英

使用 javascript 的表單驗證(帶格式的注冊表單)

[英]Form validation using javascript (Registration Form with formatting)

我正在嘗試使用定義的格式驗證每個字段並顯示適當的錯誤消息。 用戶名顯示了相應的錯誤消息,但其余代碼不“工作”。 電子郵件字段正在工作,但在我存儲變量 atEmail 和 dotEmail 后,它不再工作了。 我已經嘗試了幾天了。 非常感謝任何建議。

函數validateForm(){

var firstPassword = document.myForm.passwordOne.value;
var secondPassword = document.myForm.passwordTwo.value; 
var emailID = document.myForm.email.value;
var alphaNumeric = /^[\w ]+$/;
var letters = /^[A-Za-z]+$/;
var atEmail = emailID.indexOf("@");
var dotEmail = emailID.lastIndexOf(".");
var phone = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

 if(!alphaNumeric.test(document.myForm.userName.value)) {
    alert( "Please enter letters and numbers only!" );
    document.myForm.userName.focus() ;
    return false;
 }

 if(firstPassword.value.length < 8) {
    alert( "Password must be at least 8 characters" );
    document.myForm.passwordOne.focus() ;
    return false;
 }

 if(document.myForm.firstPassword.value != document.myForm.secondPassword.value ) {
    alert( "Password must match!" );
    document.myForm.passwordTwo.focus() ;
    return false;
 }

 if(!letters.match(document.myForm.firstName.value)) {
    alert( "Your first name should only contain letters!" );
    document.myForm.firstName.focus() ;
    return false;
 }

 if(!letters.match(document.myForm.lastName.value)) {
    alert( "Your last name should only contain letters!" );
    document.myForm.lastName.focus() ;
    return false;
 }

 if (atEmail < 1 || ( dotEmail - atEmail < 2 )) {
    alert("Please enter valid email address format: xxx@xxx.xxx");
    document.myForm.email.focus() ;
    return false;
 }

 if(phonenumber.value.match(phone)) {
    alert( "Please enter valid phone number format:(xxx) xxx-xxxx" );
    document.myForm.phonenumber.focus() ;
    return false;
 }

 if((document.myForm.signUpNewletter[0].checked == false) &&(document.myForm.signUpNewletter[1].checked == false )) {
    alert( "Please check yes or no!" );
    document.myForm.signUpNewsletter.focus() ;
    return false;
 }

 return true;

}

你確定這是正確的嗎?

if(document.myForm.firstPassword.value != document.myForm.secondPassword.value ) {

您已經定義了firstPasswordsecondPassword變量,那么為什么要在document.myForm.firstPassworddocument.myForm.secondPassword使用它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM