简体   繁体   中英

Javascript form validation

I am doing some basic form validation.

I have the following javascript function

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

<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. Is the only way to get around this to check the entire form again onsubmit?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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