2011-02-07 12 views
0

JSが正しく動作していません。どうしてか分かりません。誰でも助けてくれますか?ここに私のコードがある...ここでJavascriptフォームの検証が正しく機能していません

function validate() { 
    if(document.contactform.name.value==''){ 
     alert('Fill the Input name'); 
     name.focus(); 
     return false; 
    } 
    if(document.contactform.email.value==''){ 
     alert('Fill the Input email'); 
     email.focus(); 
     return false; 
    } 
    if(document.contactform.email.value!=''){ 
     if(!checkEmail(email.value)){ 
      alert('Please specify your correct Email!'); 
      email.focus(); 
      return false;   
     }  
    } 
    if(document.contactform.mobile.value==''){ 
     alert('Fill the Input mobile'); 
     mobile.focus(); 
     return false; 
    } 
    if(document.contactform.mobile.value!=''){ 
     if(!IsNumeric(mobile.value)){ 
      alert('Please specify your correct Mobile Number!'); 
      mobile.focus(); 
      return false;   
     }  
    } 
    if(document.contactform.subject.value==''){ 
     alert('Fill the Input Subject'); 
     subject.focus(); 
     return false; 
    } 
    if(document.contactform.message.value==''){ 
     alert('Fill the Input description'); 
     message.focus(); 
     return false; 
    } 
    if(!document.contactform.agree.checked){ 
     alert('Please check the terms and conditions'); 
     return false; 
    } 
    else{ 
     return true; 
    } 
} 

はコードだけでは名前フィールドのために働いていない

<form name="contactform" id="form" class="form" action="newmail.php" onsubmit="return validate();" method="post"> 
<TABLE align="center" border="0"> 
<TR><TD align="right"> <b>Name :</b></TD><TD align="left"><input type="text" name="name" id="name" /></TD></TR> 
<TR><TD align="right"> <b>Email :</b></TD><TD align="left"><input type="text" name="email" id="email" /></TD></TR> 
<TR><TD align="right"> <b>Mobile :</b></TD><TD align="left"><input type="text" name="mobile" id="mobile" /></TD></TR> 
<TR><TD align="right"> <b>subject :</b></TD><TD align="left"><input type="text" name="subject" id="subject" /></TD></TR> 
<TR><TD align="right"> <b>Message :</b></TD><TD align="left"><textarea name='message' id='message'></textarea></TD></TR> 
<TR><TD colspan="2" align="center"><label for="agree"><input type="checkbox" name="agree" id="agree" checked="checked"> I agree to terms and Conditions</label> </TD></TR> 
<TR><TD colspan="2" align="center"><input type="submit" value="Submit" class="submit" /> </TD></TR> 
</TABLE> 
</form> 

...私のhtmlです。私が提出した名前のコードをコメントすれば正常に動作します。何が問題なの?私の他のフォームでは、textareaフィールドだけでは機能しません。このフォームのメッセージフィールドでは、テキストエリアの検証が機能しています。

これは何が起こるかです。私がフォームを提出するとき、名前が空であれば、それは警告を表示し、直接目標ページに行く。名前の検証のためにコードにコメントすると、コードの残りの部分は関連エラーを警告することで正常に動作します。

+0

「$ DEITY」の愛については、http://stackoverflow.com/editing-helpを参照してください。 –

+0

どうしますか、しませんか?それは何を期待していますか? – Cfreak

+0

投稿が偽を返さず、送信をブロックしないようにエラーが発生しました –

答えて

1

フォームの要素には、nameという属性があります。

nameと呼ばれるinput要素を持つことはできません。

document.contactform.nameは、フォーム名またはnameという入力を参照していますか?

たとえば、入力要素を別のもの(fullname)に変更して、それをjavascriptで使用すると問題はないはずです。

+0

はい...解決策を教えてください... 'document.contactform.elements.name' – yankee

+0

こんにちはすべて...返信用Thx入力要素を変更しましたto-fullnameを使用し、そのjavascriptとその罰金を使用しました。再びオデットとすべての人にthxをしてください。 –

+0

@AdityaKumarは答えを受け取ります – footy

0

変数:name,,mobilesubjectagreeはすべて未定義です。 IEがIDを持つ要素からグローバル変数を作成することを除いては、それを使用しないでください。一部のブラウザは、それらのグローバルを作成しません。あなたのスクリプトを書く方法は次のとおりです。

function validate() { 
    var name = document.getElementById('name'); 
    if(!name.value){ 
     alert('Fill the Input name'); 
     name.focus(); 
     return false; 
    } 

    var email = document.getElementById('email'); 
    if(!email.value){ 
     alert('Fill the Input email'); 
     email.focus(); 
     return false; 
    } 

    // No need to check if (email.value) again 
    if(!checkEmail(email.value)){ 
     alert('Please specify your correct Email!'); 
     email.focus(); 
     return false; 
    } 

    var mobile = document.getElementById('mobile'); 
    if(!mobile.value){ 
     alert('Fill the Input mobile'); 
     mobile.focus(); 
     return false; 
    } 

    //No need to check if mobile.value again 

    if(!IsNumeric(mobile.value)){ 
     alert('Please specify your correct Mobile Number!'); 
     mobile.focus(); 
     return false; 
    } 

    var subject = document.getElementById('subject'); 
    if(!subject.value){ 
     alert('Fill the Input Subject'); 
     subject.focus(); 
     return false; 
    } 

    var message = document.getElementById('message'); 
    if(!message.value){ 
     alert('Fill the Input description'); 
     message.focus(); 
     return false; 
    } 

    var agree = document.getElementById('agree'); 
    if(!agree.checked){ 
     alert('Please check the terms and conditions'); 
     return false; 
    } 

    // No need for an else here 
    return true; 
} 
0

フォームオブジェクトの属性によるフォームフィールドの参照は、要素が別の属性の名前と一致しない場合にのみ機能します。

"name"はフォームの属性なので、
document.contactform.name.valueは実際には "name"というフォーム要素ではなく連絡先フォームの "name"属性を参照しています。

フィールドの名前を「fullname」などの名前に変更するか、idsおよびdocument.getElementByIdを使用してフォームフィールドにアクセスすることをお勧めします。

関連する問題