正在检查标签文本,并验证与其绑定的输入字段是否至少包含x个数量的字符

Checking label text and verifying that input field tied to it has at least x amount characters

本文关键字:是否 字段 包含 字符 输入 文本 标签 检查 验证 绑定      更新时间:2023-09-26

我对这种语言非常陌生。我有能力检查标签刺痛,但我想确保它旁边的输入字段至少有6位数字(如果可能的话是数字)

$(document).ready(function(){
    $("label:contains('6 digit')") //i get lost here
});

标记:

<form>
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>6 digit</label><input value="1" type="text" class="check"> 
    <label>6 digit</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <input type="submit">
</form>

Tim Bolton几乎做对了,尽管他正在检查值是否低于5,但他没有测试位数。我会这样做:

function isValidForm(){
   var labels = $('form labels');
   $(labels).each(function(){
      var inputText = $(this).next();
      if($(inputText).val().length < 6 || isNaN($(inputText).val())) {
         $(inputText).css('color', 'red'); // you can do whatever you want to show the field is not properly filled
         return false;
      }         
   })
   return true;
}

然后,在提交表格时,你所要做的就是:

$('form').submit(function(){
   if(!isValidForm()){
      alert('please fix the errors');
      return false;
   }
});

您将需要在label上设置for属性。您还需要为label识别的输入设置一个ID。这应该用合适的钩子武装你,让你做你想做的事(或者至少设置得合适)。

使用您当前的设置,尝试:

<script>
    $(document).ready(function(){
        $('#submit').click(function(event) {
            event.preventDefault();
            $('label').each(function() {
                var len = $(this).next().length;
                if(len < 5) { 
                    alert('Too Short!');
                } else { $('#form').submit();
            });
        })
    });
</script>
<form id="form">
    <label>Test</label><input value="asdfassd1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>6 digit</label><input value="1" type="text" class="check"> 
    <label>6 digit</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <input type="submit" id="submit">
</form>

label的目的是通过id:与表单相关元素相关联

<label for="digits">Enter Digits</label>
<input type="text" id="digits" />

有关标签,请参阅文档。假设所有的input都应该有一个id,如果它们有标签的话,请尝试以下操作:

$('#inputID').each(function() {
    if (!$(this).val().match(/^'d{6,}$/)) {
        alert('incorrect formatting!');
    }
});

它根据正则表达式测试所选输入的值,以获得"所有数字,至少六个"。

  • jsFiddle演示

我认为正则表达式是验证简单输入的最简洁和可移植的方法。

如果可能的话,找到一种简单的方法来区分需要验证的字段,比如Tim Bolton建议的元素上的ID属性,或者可能通过下面的CSS类(即"needs6"类):

<form>
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>6 digit</label><input value="1" type="text" class="check needs6"> 
    <label>6 digit</label><input value="1" type="text" class="check needs6">
    <label>Test</label><input value="1" type="text" class="check">
    <input type="submit" />
</form>

然后您的jQuery选择器变得简单多了(即$("input.needs6"))。

一旦选择了输入,就有很多方法可以验证它们。就我个人而言,我会使用以下方法:

$(document).ready(function() {
    var testFor6 = function () {
        var allHave6 = true;
        $("input.needs6").each(function () {
            var input = $(this);
            var value = input.val();
            if (!value.match(/'d{6}/g)) {
                allHave6 = false;
                input.addClass('doesNotHave6');
            } else {
                input.removeClass('doesNotHave6');
            }
        });
        return allHave6;
    };
    $('input[type="submit"]').click(function (e) {
        var allHave6 = testFor6();
        if (!allHave6) {
            e.preventDefault(); //Cancel event-bubbling.
        }
        return allHave6;        //Prevent form submission when false.
    });
});

如果你想看到这一点,请将其弹出http://jsfiddle.net/并对其进行测试,尽管您可能希望将其与一些示例CSS类相结合以查看其工作情况。下面的示例。

.needs6 {background-color: yellow;}
.doesNotHave6 {background-color: red;}

希望这能有所帮助,

Pete

附录:

你是说空间吗。。。

<form>
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <!-- here? --><label><!-- here? -->6 digit</label><!-- here? --><input value="1" type="text" class="check needs6" value="<!-- here? -->"> 
    <label>6 digit</label><input value="1" type="text" class="check needs6">
    <label>Test</label><input value="1" type="text" class="check">
    <input type="submit" />
</form>

请澄清。


当然!

只需将输入循环函数更改为以下任意部分。。。

$("input.needs6").each(function () {
    var input = $(this);
    var value = input.val();
    //You can use this to trim.
    var trimmedValue = value.replace(/'s's*$/, '').replace(/^'s's*/, '');
    //You can use this to find leading whitespace.
    var hasLeadingSpaces = value.match(/^'s's*/, '');
    //You can use this to find trailing whitespace.
    var hasTrailingSpaces = value.match(/'s's*$/, '');
    if (hasLeadingSpaces) {
        //Do something
    }
    if (hasTrailingSpaces) {
        //Do something
    }
    //Check original value
    if (!value.match(/'d{6}/g)) {
        allHave6 = false;
        input.addClass('doesNotHave6');
    } else {
        input.removeClass('doesNotHave6');
    }
    //Check trimmed value
    if (!trimmedValue.match(/'d{6}/g)) {
        allHave6 = false;
        input.addClass('doesNotHave6');
    } else {
        input.removeClass('doesNotHave6');
    }
});