简体   繁体   English

html表单属性和验证

[英]html form attributes and validation

Im trying to figure out basic HTML form validation by looking at a bunch of different examples. 我试图通过查看许多不同的示例来弄清楚基本的HTML表单验证。

I came across an example used by Kajabi, there's an example at http://megaph.com/pages/39841 我遇到了Kajabi使用的示例, http: //megaph.com/pages/39841中有一个示例

When when I look at form tag it contains this rather strange tag ql-optin. 当我查看表单标签时,它包含这个相当奇怪的标签ql-optin。 If I remove this tag, the form validation stops working, but the form redirects to aweber. 如果删除此标记,则表单验证将停止工作,但表单将重定向到aweber。 If I leave it in, the validation works, but the form wont post. 如果我将其保留,则验证有效,但该表单不会发布。

its none of the usual attributes, and its presumably not CSS 它没有任何通常的属性,大概不是CSS

Im just curious what this is, and why it works. 我只是好奇这是什么,以及它为什么起作用。 Anyone got any ideas ? 任何人有任何想法吗?

This isn't anything standard. 这不是什么标准。 They're just adding arbitrary attributes and using JavaScript to work with them. 他们只是添加任意属性,并使用JavaScript来处理它们。 Taking a look at their JS shows lines like this.emailInput=this.formEl.find('[ql-id="optin_email"] input') . 看一下他们的JS,会显示如下this.emailInput=this.formEl.find('[ql-id="optin_email"] input')

As ceejayoz points out, their validation is customized and will be difficult to deconstruct. 正如ceejayoz指出的那样,它们的验证是自定义的,将难以解构。 If you are trying to understand form validation, that is not the best example to study. 如果您想了解表单验证,那不是学习的最佳示例。

If you just want to implement form validation yourself, here is an alternative "build your own" form validation - very simple. 如果您只想自己实现表单验证,这是一种“构建自己的”表单验证的替代方法-非常简单。

It stores the field labels and IDs in an array, then loops through the array to test each field. 它将字段标签和ID存储在一个数组中,然后循环遍历该数组以测试每个字段。 When done, it reports which fields failed validation. 完成后,它将报告哪些字段验证失败。

jsFiddle Demo jsFiddle演示

HTML: HTML:

One: <input type="text" id="f1"><br />
Two: <input type="text" id="f2"><br />
Three: <input type="text" id="f3"><br />
Four: <input type="text" id="f4"><br />
<br />
<input type="button" id="mybutt" value="Go" />

jQuery : jQuery的

var chkFld, arrAll = {'One':'f1','Two':'f2','Three':'f3','Four':'f4'};

$('#mybutt').click(function() {
    var errMsg='', badFlds='', firstBad='';
    for(var key in arrAll){
        chkFld = '#'+arrAll[key];
        $(chkFld).removeClass('error');
        if ($(chkFld).val() ==''){
            $(chkFld).addClass('error');
            //alert('Please complete field ' + arrAll[key] );
            errMsg += '*' + key + '\n';
            if (firstBad=='') firstBad=chkFld;
        }
    }
    if (errMsg != '') {
        alert(errMsg);
        $(firstBad).focus();
    }
}); //END mybutt.click

CSS: CSS:

.error{border:1px solid red;background:yellow;}

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

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