English 中文(简体)
当单项检查无效时,打字证明书被打断。
原标题:Form validation is interrupted when one check is invalid in javascript

我拿着一个包含两个文本的表格,每个表格都涉及一组邮件。

<form name="myform" action= entryupdate.php  method="post">
    <textarea name="mailgroup1" rows="2" cols="50" onchange="checkFormValue();">
    </textarea>
    <textarea name="mailgroup2" rows="2" cols="50" onchange="checkFormValue();">
    </textarea>
    <input name="update" type="submit" value="Update description"/>
</form>

而且,我有责任根据我们的内部规范检查电子邮件是否格式良好。

function checkmail(component){
    var emailpattern = /^[A-z0-9._-]+@[A-z0-9][A-z0-9-]*(.[A-z0-9_-]+)*.([A-z]{2,6})$/;
    var mails = component.value.split(/[

	 ]+/);
    var valid = true;
    for(var i=0; i<mails.length; i++){
        valid = valid && emailpattern.test(mails[i]);
        alert("Mail: "+mails[i]+" Valid: "+ emailpattern.test(mails[i]));
    }
    if(valid){
        component.setAttribute( class ,  valid );
    }else{
        component.setAttribute( class ,  invalid );
    }
    return valid;
}

如果某个领域被归类为无效,则采用以下方式:

.invalid
{
background-color:#fffacd;
}

如果在文本中的一项修改了价值,则要求履行以下职能,即检查任何价值是否正确格式,如果情况是,提交纽伦残疾。

function checkFormValue(){
    var validform = true;
    validform = validform && checkmail(document.myform.mailgroup1) && checkmail(document.myform.mailgroup2);
    document.hotfixomat.update.disabled = !validform;
}

问题是,如果第一次检查是假的,那么第二次检查就没有进行,如果发生价值没有正确格式的情况,那么这种改变就没有发生。 (但提交纽伦为残疾)。 为什么检查中断?

最佳回答

其原因,是您如何在最后一行获得<代码>validform变量。 Java本像其他许多语文一样,不会在蓝皮条码上进一步。 如果可能的话:

var validform = true;
validform = validform && checkmail(document.myform.mailgroup1) && checkmail(document.myform.mailgroup2);

如果第一个<代码>checkmail(>)是假的,那么它就不必履行第二项义务,因为没有可能的办法,validform将属实。 如果你设立<条码>瓦尔有效格式 = 虚假,那么它就赢得了任何支票功能。

例如:

如果你想要确保两者都得到呼吁,那么你可以分裂,并做如下事情:

var validForm1 = checkmail(document.myform.mailgroup1),
    validForm2 = checkmail(document.myform.mailgroup2),
    validForm = validForm1 && validForm2;

或者,你可以改变方法,以便通过你想要验证的所有要素,改变一个变量和回报。

基本实例:

function checkmailElements(myarray){
    var returnVal = true;

    for(var i = 0; i< myarray.length; i++){
        if( !checkmail(myarray[i]) ){
            returnVal = false;
        }
    }

    returnVal;
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签