简体   繁体   English

JavaScript表单验证

[英]Javascript form validation

I am doing some basic form validation. 我正在做一些基本的表单验证。

I have the following javascript function 我有以下javascript函数

function fullField(x,span_id)
{
var result=false;
if(x.value==0)
{
    document.getElementById(span_id).innerHTML =" Required";
    result=false;
}else{
    document.getElementById(span_id).innerHTML="";//can use tick <img src='images/site_images/tick.png' />
    result=true;
}

return result;
}

I have an input which is checked onblur 我有一个输入检查onblur

<input type='text' onblur='return fullField(this,'span1')name='first_name' />
<span id='span1'></span>

The function works, writing 'Required' into the span if the person tabs off the field without filling it in. However, when i click submit the form still submits. 该功能有效,如果人员在未填写的情况下退出该字段,则在跨度中写入“必填”。但是,当我单击“提交”时,表单仍会提交。 I think i am missing some fundamental point here because i though that if any of the fields in my form return false then the form would not submit. 我想我在这里遗漏了一些基本要点,因为我虽然如果表单中的任何字段返回false,那么表单都不会提交。 Is the only way to get around this to check the entire form again onsubmit? 解决此问题的唯一方法是在提交时再次检查整个表单吗?

您可以在页面上创建一个变量,如果对字段的验证失败,则将其设置为false,然后以onSubmit(return(myValidationVariable))的形式设置-不是最优雅的设计,但应该可以与当前设置很好地配合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM