简体   繁体   English

验证薪水输入jQuery

[英]Validate salary input jQuery

I'm trying to validate input value must be like 1.400,00 and 12.000,00. 我正在尝试验证输入值必须像1.400,00和12.000,00。 If input has the correct value, it should remove the disabled class from the else stays disabled. 如果输入具有正确的值,则应从其余保持禁用状态的状态中删除禁用的类。

I tried like this but did't get success :( 我试图这样,但没有成功:(

<input id="ex2" class="salary" type="number" placeholder="1.400,00 - 12.000,00" name="salaryRange2"/>

<a href="#" id="checkSalary1" class="btn next disabled">Next Step</a>

$("#ex2").on("keyup", function(){
    var valid = /^\d{1,6}(?:\.\d{0,2})?$/.test(this.value),
        val = this.value;

    if(valid){
        console.log("Invalid input!");
        this.value = val.substring(0, val.length - 1);
        $("#checkSalary1").removeClass("disabled");
    }
    else{
        $("#checkSalary1").addClass("disabled");
    }
});

Can anyone help how can achieve this condition? 谁能帮忙达到这个条件?

Thanks in advance 提前致谢

Your regex is way out, something like this gets you a little closer: 您的正则表达式出路,像这样使您更近一些:

^\\d{1,2}.\\d{3},\\d{2}$ ^ \\ d {1,2}。\\ d {3},\\ d {2} $

Which looks for: 哪个寻找:

  • 1-2 digits 1-2位
  • a literal . 一个文字.
  • 3 digits 3位数
  • a literal , 文字,
  • 2 digits 2位数

You may like to enhance this to make the decimal portion optional. 您可能希望对此进行增强,以使小数部分为可选。

From there, you need to actually parse the string to a number to check it is within the valid numerical range (as, for example 95.000,00 would pass on a regex check, but not in the range check. 从那里开始,您实际上需要将字符串解析为一个数字,以检查它是否在有效的数字范围内(例如, 95.000,00将通过正则表达式检查,而不是范围检查。

The number input value is a ... Number , so you can just use a numeric comparison, no RegExp needed. 数字输入值为... ... Number ,因此您可以使用数字比较,而无需RegExp Furthermore, the keyup event will not trigger if you use a mouse to increment or decrement its value. 此外,如果使用鼠标增加或减少其值,则keyup事件将不会触发。

Here's a snippet that continuously checks the salary input value, enables or disables the button as applicable and shows a message about the input value validity. 这是一个片段,可不断检查薪水输入值,在适用时启用或禁用按钮,并显示有关输入值有效性的消息。 To make your live even easier, for a [type=number] -input you can also set a minimum and maximum value by the way (used in the snippets checkValue method). 为了使您的生活更加轻松,对于[type=number] -input,您还可以顺便设置一个最小值和最大值(在代码段中使用checkValue方法)。

See also 也可以看看

 (() => { const inputElement = document.querySelector("#ex2"); const report = document.querySelector("#report"); const bttn = document.querySelector("button"); const getAction = cando => cando ? "removeAttribute" : "setAttribute"; const checkVal = val => val && val >= +inputElement.getAttribute("min") && val <= +inputElement.getAttribute("max"); const messages = { cando: "Ok to submit", noValue: "Waiting for input (1.400,00 - 12.000,00)", invalid: "Value should be from 1.400,00 up to 12.000,00" }; inputElement.focus(); checkValue(); function checkValue() { const val = inputElement.value; const cando = checkVal(val); report.innerHTML = !val ? messages.noValue : cando ? messages.cando : messages.invalid; bttn[getAction(cando)]("disabled", true); return setTimeout(checkValue, 200); } })(); 
 <input id="ex2" class="salary" type="number" min="1400" max="12000" step="0.01" name="salaryRange2"/> <button disabled>submit</button> <span id="report"></span> 

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

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