简体   繁体   English

可以在一个字段上以语义UI形式的验证规则在另一个字段上为条件吗?

[英]Can semantic-ui form validation rules on one field be conditional on a different field?

Let's say I have a form with two fields. 假设我有一个包含两个字段的表单。 I'd like the following to be possible: 我希望以下是可能的:

Submit the form empty - Validation succeeds Both fields contain data - Validation succeeds Field one has data, field two empty - Validation succeeds Field one empty, field two has data - Validation fails 提交表单为空-验证成功两个字段都包含数据-验证成功字段一为数据,字段二为空-验证成功字段一为空,字段二为数据-验证失败

Is there a way to do this? 有没有办法做到这一点?

My server code already validates for this, but I'd love to be able to prevent the client from hitting the server if it doesn't have to.. but if that's not possible, is there a way to pass form validation error messages to the form as the page loads? 我的服务器代码已经对此进行了验证,但是我很乐意能够阻止客户端在不需要的情况下访问服务器..但是,如果不可能的话,有没有办法将表单验证错误消息传递给页面加载的形式?

Thanks!! 谢谢!!

you can use regex for complex validation rule 您可以将正则表达式用于复杂的验证规则

first add some code to form.js, this file is accessible in: 首先向form.js添加一些代码,该文件可以在以下位置访问:

Semantic-UI-master\\Semantic-UI-master\\dist\\components\\form.js 语义UI主机\\语义UI主机\\ dist \\ components \\ form.js

after this code 在此代码之后

// is most likely an email
email: function(value){
  var
    emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", "i")
  ;
  return emailRegExp.test(value);
},

add this code 添加此代码

// is most likely an my regex
regex: function(value,reg){
  var
    regexRegExp = new RegExp(reg, "i")
  ;
  return regexRegExp.test(value);
},

and use it in your semantic ui code, like this 并在您的语义ui代码中使用它,例如

firstname: {
    identifier: "first_name",
    optional: true,
    rules: [
        {
            type: "regex[^[a-zA-Z -]+$]",
            prompt: "First name can only consist of letters, spaces and dashes"
        }
    ]
}

for example this is my code to validate a complex cod contain only 10 digit or alphabet in mix 例如,这是我的代码,用于验证仅包含10位数字或字母的复杂鳕鱼

            $("form[data-id=\"2\"]").form({
                v: { identifier: 'active_code', rules: [{ type: "regex[^[a3-z8]{10}$]", prompt: 'کد فعال سازی خود را وارد کنید'}] }
            },
            {
                inline: true,
                on: 'blur',
                transition: 'swing left',
                duration: 1000,
                onSuccess: formOnSuccess
            }
            );

the complete code 完整的代码

        $("form[data-id=\"1\"]").form({
            f: { identifier: 'family', rules: [{ type: 'empty', prompt: 'فامیلی خود را وارد کنید'}] },
            e: { identifier: 'email', rules: [{ type: 'email', prompt: 'ایمیل خود را وارد کنید'}] }
        },
            {
                inline: true,
                on: 'blur',
                transition: 'swing left',
                duration: 1000,
                onSuccess: formOnSuccess
            }
        );
        $("form[data-id=\"2\"]").form({
            v: { identifier: 'active_code', rules: [{ type: "regex[^[a0-z9]{10}$]", prompt: 'کد فعال سازی خود را وارد کنید'}] }
        },
            {
                inline: true,
                on: 'blur',
                transition: 'swing left',
                duration: 1000,
                onSuccess: loadPersonal
            }
        );

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

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