大佬教程收集整理的这篇文章主要介绍了javascript – Angular 2 Custom Validator:检查输入值是否为整数?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用number(control.value)为空字段返回0 – 不好.
或者不考虑空格的parseInt(control.value,10):
如果我有类似的东西:1 space 0,24 = 1,024它返回1 – 它通过验证器没有错误.
Lodash的功能如下:_.isInteger(control.value)或_.isNumeric(control.value)
//每次返回false – 这是预期的,因为输入值是字符串而不是数字.
组合这样的方法会产生一个带有许多if / else语句的混乱函数,即便如此,我也不确定我是否得到了所有边缘情况.我当然需要更直接的方法.有任何想法吗?
app.component.html:
<input formControlName="mynumber">
app.component.ts:
export class AppComponent { mynumber:FormControl constructor(private _Validatorsservice: ValidatorsservicE){ } this.mynumber= new FormControl('DefaultValue',[ Validators.required,this._Validatorsservice.isInteger ]); }
function check_if_is_Integer(value){ if((parseFloat(value) == parseInt(value)) && !isNaN(value)){ // I can have spacespacespace1 - which is 1 and validators pases but // spacespacespace doesn't - which is what i wanted. // 1space2 doesn't pass - good // of course,when saving data you do another parseInt. return true; } else { return false; } } @Injectable() export class Validatorsservice { public isInteger = (control:FormControl) => { return check_if_is_Integer(control.value) ? null : { notNumeric: true } } }
请享用!
以上是大佬教程为你收集整理的javascript – Angular 2 Custom Validator:检查输入值是否为整数?全部内容,希望文章能够帮你解决javascript – Angular 2 Custom Validator:检查输入值是否为整数?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。