简体   繁体   English

检查输入字段(angularJs)上动态增量的正则表达式?

[英]Regular expression that checks dynamic increments on input fields (angularJs)?

I have to validate the user input of an input field (type number) with a regex. 我必须使用正则表达式验证用户输入的输入字段(类型编号)。

I reveice the following dynamic values: 我揭示了以下动态值:

  • minimum: i'm already validating that with angular 最少:我已经用角度验证了
  • maximum: i'm already validating that with angular 最大:我已经用角度验证了
  • increment: <- my problem 增量:<-我的问题

An example: minimum: 10; 示例:最小值:10; maximum: 100; 最大值:100; increment: 10 增量:10

Allowed user input with the dynamic increment of 10 should be: 动态增量为10的允许用户输入应为:

10 - 20 - 30 - 40 - 50 - 60 - 70 - 80 - 90 - 100 10-20-30-40-50-60-70-80-90-100

Other examples: 其他例子:

  • minimum: 0; 最小:0 maximum: 2000; 最大值:2000; increment: 100 增量:100
  • minimum: 1; 最低:1; maximum: 3.4; 最大值:3.4; increment: 0.2 增量:0.2

I have tried several regex expressions, but not even one worked with the dynamic values (different number length/decimal). 我尝试了几种正则表达式,但甚至没有一种使用动态值(不同的数字长度/小数)。 Maybe it's easier to have one with numbers and one for numbers with decimal. 也许有一个带数字的数字和一个带小数的数字更容易。

Thank you for your help! 谢谢您的帮助!

Use input tag with proper attributes. 使用具有适当属性的input标签。 HTML5 provides you with a step attribute . HTML5为您提供了step属性

 <input type="number" min=10 max=100 step=10 /> <input type="number" min=0 max=2000 step=100 /> <input type="number" min=0 max=3.4 step=0.2 /> 

Fast answer to your question (since you said angular, I assume you need it for JS): 快速回答您的问题(因为您说的是棱角分明,所以我认为您需要JS):

function tenToHundred(num){
    return /^[1-9]{1}0{1}$|^100$/.test(num);
}
for(var i=1;i<=11;i++)console.log(tenToHundred(i*10));//10 ~ 100 test

Suggestion about those kind of checks (based on your examples), REGEX as a tool is for string patterns checks\\matches etc... It's less suitable for numeric calculations so perhaps you should consider other methods for validation. 关于这类检查的建议(根据您的示例),REGEX作为工具用于字符串模式检查\\匹配等。它不适合用于数值计算,因此也许您应该考虑其他验证方法。 for example - using remainder operator 例如-使用余数运算符

function tenToHundred(num){
    var min=10, max=100, increment=10;//when need to maintain - just change those
    return num>=min && num<=max && num%increment==0;
}
for(var i=1;i<=10;i++)console.log(tenToHundred(i*10));//10 ~ 100 tests

This way it will be easier to maintain you code. 这样,将更易于维护代码。

you are right. 你是对的。 Now I'm setting the step value as hjpotter92 and added some logic as Nikita Kurtin mentioned. 现在,我将步长值设置为hjpotter92,并添加了Nikita Kurtin提到的一些逻辑。 Angular is not able to validate the step out of the box.. Angular无法验证开箱即用的步骤。

<input type="number" step="10" validate-step />

Then I wrote a parser: 然后我写了一个解析器:

angular
    .module( '....validateStep', [] )
    .directive( 'validateStep', ValidateStepDirective );

/**
 * @namespace ValidateStepDirective
 * @example <validate-step></validate-step>
 * @memberOf Directives
 */
function ValidateStepDirective(){
    return {
        require: 'ngModel',
        link: ValidateStepDirectiveController
    };
}

function ValidateStepDirectiveController( scope, element, attrs, ctrl ){
    var step;
    if( attrs.step) {
        step = parseFloat( attrs.step );
    }

    function isValidStep( value, step ) {
        var checkValue = value * 100;
        // fixing js multiplication issue (2.3*100=229.999999)
        checkValue = checkValue.toFixed(0);
        var checkStep = step * 100;
        return checkValue%checkStep  === 0;
    }

    function stepValidator(viewValue){
        if( step && isValidStep(viewValue, step) ){
            ctrl.$setValidity( 'step', true );
            return viewValue;
        }else{
            ctrl.$setValidity( 'step', false );
            // if invalid, return undefined
            // (no model update happens)
            return;
        }
    }

    ctrl.$parsers.unshift( stepValidator );
}

I hope I can help others with my solution. 我希望我可以为他人提供解决方案。

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

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