需要javascript验证和提交

时间:2011-10-17 08:21:17

标签: javascript html javascript-events

我已经完成了一定数量的验证,但无法阻止表单提交,请帮我验证其他左侧字段,这是我的代码:

<form action="" method="post" id="contacts-form" onsubmit="return >
<fieldset>
<div class="field">
<input type="text" value="Name:" id="name" onfocus="if(this.value=='Name:'){this.value=''}" onblur="if(this.value==''){this.value='Name:'}" />`enter code here`
</div>
<div class="field">
<input type="text" value="E-mail:" id="email" onfocus="if(this.value=='E-mail:'){this.value=''}" onblur="if(this.value==''){this.value='E-mail:'}" />
</div>
<div>
<textarea cols="1" rows="1" id="message" onfocus="if(this.value=='Message:'){this.value=''}" onblur="if(this.value==''){this.value='Message:'}">Message:</textarea>
</div>
<div class="link"><a href="#" onclick="document.getElementById('contacts-form').reset()">Clear</a> &nbsp; &nbsp; <a href="#" onclick="document.getElementById('contacts-form').submit(validateForm();)">Send</a></div>'

我使用的Js代码是

function validateForm()
{
var x=document.forms["contacts-form"]["email"].value;
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;
}
}

4 个答案:

答案 0 :(得分:0)

 change onsubmit="return >
         onsubmit="return validateForm();"

并关闭表格和字段集标签,如下所示&lt; / form&gt; &LT; / fieldset&gt;

答案 1 :(得分:0)

这样做:

onsubmit="return >替换为onSubmit="return validateForm();"

替换

<div class="link"><a href="#" onclick="document.getElementById('contacts-form').reset()">Clear</a>    &nbsp; &nbsp; <a href="#" onclick="document.getElementById('contacts-form').submit()">Send</a></div>

您现在在提交时执行validateForm,如果失败,onSubmit将从您的函数中获取false,并且不会提交/重新加载该网站。

我不确定是否有必要,但您也可以在if之后在函数中添加return true

答案 2 :(得分:0)

尝试这样的事情(它可能有一些错误,因为我在没有检查的情况下编写它):

function validateForm(){
var x=document.forms.contacts-form.email.value;
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;
}
else {
    document.forms.contacts-form.submit();
}
}

和html:

<form action='' name='contacts-form' method='post'>
<fieldset>
<div class="field">
<input type="text" value="Name:" id="name" onfocus="if(this.value=='Name:')            {this.value=''}" onblur="if(this.value==''){this.value='Name:'}" />`enter code here`
</div>
<div class="field">
<input type="text" value="E-mail:" id="email" onfocus="if(this.value=='E-mail:')        {this.value=''}" onblur="if(this.value==''){this.value='E-mail:'}" />
</div>
<div>
<textarea cols="1" rows="1" id="message" onfocus="if(this.value=='Message:')        {this.value=''}" onblur="if(this.value==''){this.value='Message:'}">Message:</textarea>
</div>
<div class="link"><a href="#" onclick="document.getElementById('contacts-vform').reset()">Clear</a> &nbsp; &nbsp; <button value="Send" onclick="validateForm()"></div>
</fieldset>
</form>

答案 3 :(得分:0)

尝试这样:

function validateForm()
{
var isValid =true;
var x=document.forms["contacts-form"]["email"].value;
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");
isValid= false;
}
retrun isValid;
}

function validateForm() { var isValid =true; var x=document.forms["contacts-form"]["email"].value; 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"); isValid= false; } retrun isValid; }