所有的输入字段验证通过单个/最小行代码与jQuery [英] all input field validation through single/minimum line on code with jQuery

查看:145
本文介绍了所有的输入字段验证通过单个/最小行代码与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中有30个输入框,10个文本区域和3个选择框( updated )字段,并且都是必填字段。



如何通过jQuery / javascript将<不能为空/空/ NULL的所有输入字段应用于验证。



我不想每次使用每个输入框的ID,并为每个输入框和文本区域创建单独的验证。



如果任何输入字段都是空白的,那么它的边界就会变成红色,并且当用户去写东西的时候就会变成正常的(无边界即删除那个类)。
$ b

UPDATE

strong>:



还有一些选择框,如何将它们一起验证?

解决方案


  1. 从每个文本,
    textarea和列表框字段中删除红色边框。

  2. 过滤掉包含至少1
    非空白字符的
    元素 - \ S +

  3. 添加红色$
















    $ b $> function validate(){
    return $(input:text,textarea,select)。removeClass('blank')。filter(function(){
    return! \\ S + /。test($(this).val());
    })。addClass('blank')。size()== 0;
    }

    $('#form')。submit(validate);

    css

      .blank {
    border:1px红色实体;
    }


    I have a form in which I have 30 input boxes and 10 text areas and 3 select boxes (updated) field and all are mandatory fields.

    How do I apply validation through jQuery /javascript together for all input fields that it can't be blank/empty/NULL.

    I don't want to use each time each input box's ID and create a separate validation for each input box and text area.

    If any input field is blank then it's border become red and when a use going to write something then become normal( no border i.e. remove that class)

    UPDATE:

    There are also select boxes ..how do I validate them together?

    解决方案

    1. Remove the red border from each text, textarea, and list box field.
    2. Filter out elements that contain at least 1 non-whitespace character - \S+
    3. Add a red border to the remaining elements

    javascript

    function validate() {
      return $("input:text,textarea,select").removeClass('blank').filter(function() {
        return !/\S+/.test($(this).val());
      }).addClass('blank').size() == 0;
    }
    
    $('#form').submit(validate);
    

    css

    .blank {
        border: 1px red solid;
    }
    

    这篇关于所有的输入字段验证通过单个/最小行代码与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆