-1

HTML

<form name="f1" action="feedback1.php" method="Post" onSubmit="return isDataFilled();" >
    <table border="0" align="center" width="500px" style="max-width: 500px;" cellspacing="3" cellpadding="5" align="center">
        <tr align="left">
            <td width="25%">
                Enter your subject            </td>
            <td width="75%"><input type="text" name="subject" size="30" value="Your subject" onClick="if(this.value=='Your subject'){this.value=''}; this.style.backgroundColor='#CCFF99'" onBlur="if(this.value==''){this.value='Your subject'}; this.style.backgroundColor='white'"/></td>
        </tr>
        <tr align="left">
            <td>
                Enter your email<span style="color:#FF0000">*</span>            </td>
            <td>        
                <input type="text" name="email" size="30" value="example@mail.com" onClick="if(this.value=='example@mail.com'){this.value=''}; this.style.backgroundColor='#CCFF99'" onBlur="if(this.value==''){this.value='example@mail.com'}; this.style.backgroundColor='white'"/>            </td>
        </tr>
        <tr align="left">
            <td colspan="2">
                Enter your message here<span style="color:#FF0000">*</span>:            </td>
        </tr>
        <tr align="left">
            <td colspan="2">
                <textarea rows="10" cols="50" name="message" title="Your message goes here" onClick= "if(this.value=='Your message goes here'){this.value=''}; this.style.backgroundColor='#CCFF99'" onBlur="if(this.value==''){this.value='Your message goes here'}; this.style.backgroundColor='white'" >Your message goes here</textarea>            </td>
        </tr>
        <tr>
            <td colspan="" align="right">
                <input type="submit" value="Send" name="b1" title="Send your message"/>
            </td>
            <td align="center">          
                <input type="reset" value="Reset" name="reset" title="Removes your form data and fill it again"/>            </td>
        </tr>
    </table>
</form

JavaScript

function isDataFilled()
{  
    if(document.forms['f1']['email'].value=='example@mail.com')
    {
        alert("No email address in email field!");
        return false;
    }
    if(document.forms['f1']['message'].value=='Your message goes here')
    {
        alert("No message in message field!");
        return false;
    }
    return isEmailCorrect(document.forms["f1"]["email"].value);
    return check_word_length(document.forms['f1']['message'].value, 20);
}

function isEmailCorrect(f_email)
{
    var x=f_email;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
      alert("Not a valid e-mail address");
      return false;
    }
}
function check_word_length(text, over_size)
{
    var word=0;
    var message=text;
    for(i=0;i<message.length;i++)
    {
        if(message.charAt(i)==" ")
        {
            word=0;            
        }
        else
        {
            word++;
            if(word>=over_size)
            {
                alert("Too long text entered");
                return false;
            }
        }
    }
}

機能check_word_length(text, over_size)が動作していません。私のコードは大丈夫だと思うので、私は混乱しています。

4

3 に答える 3

4

あなたの最後にisDataFilled

return isEmailCorrect(document.forms["f1"]["email"].value);
return check_word_length(document.forms['f1']['message'].value, 20);

キーワードは、現在のreturn関数をすぐに終了します。したがって、returnそのコードの 2 番目に到達することはありません。

于 2013-08-19T17:44:28.857 に答える
0

この問題は、 isDataFilled が isEmailCorrect() が返すものを返すという事実によって引き起こされていると思います。関数の実行はそこで停止するため、最後の関数を呼び出すことはありません

于 2013-08-19T17:46:40.360 に答える