简体   繁体   English

jQuery 验证只工作一次问题

[英]jQuery validation working just once problem

I have a problem with validation我有验证问题

My validation script is working just once and after that, it doesn't work.我的验证脚本只运行一次,之后就不起作用了。

 $(document).ready(function() { function validateCurrency($currency) { var currencyFormat = /^[0-9]{1,3}(,[0-9]{3})*\\.[0-9]+$/; return currencyFormat.test($currency); } $(document).on("change", "#price_meter", function() { if ($("#price_meter").val() === '' && !validateCurrency($("#price_meter").val())) { $("#price_meterError").html('لطفا قیمت متری ملک را به تومان وارد کنید.'); } else { $("#price_meterError").html(''); } }); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <label for="price_meter">قیمت متری:</label> <input type="text" placeholder="قیمت متری ملک" class="form-control divide" name="price_meter" id="price_meter"> <div id="price_meterError" class="text-danger mt-1 validate-error"></div>

我认为你应该像这样在你的条件中将你的双符号(&&)更改为双管道(||):

if ($("#price_meter").val() === '' || !validateCurrency($("#price_meter").val())){

You need to use OR operator ||您需要使用 OR 运算符|| instead of AND && in your condition.而不是 AND &&在你的条件下。

So your test will say if the current value is empty or is not match the pattern show the message else clear the message.因此,您的测试将说明当前值是否为空或与模式不匹配,否则显示消息则清除消息。

$(document).on("input", "#price_meter", function() {
    if ( $(this).val() === '' || !validateCurrency( $(this).val()) ) {
    __________________________^^
        $("#price_meterError").html('لطفا قیمت متری ملک را به تومان وارد کنید.');
    } else {
        $("#price_meterError").html('');
    }
});

NOTE 1: You could get the current value using this keyword like $(this).val() .注意 1:您可以使用this关键字(如$(this).val()获取当前值。

NOTE 2: I suggest the use of input event instead of change since it's more efficient when tracking the user inputs.注意 2:我建议使用input事件而不是change因为它在跟踪用户输入时更有效。

 $(document).ready(function() { function validateCurrency($currency) { var currencyFormat = /^[0-9]{1,3}(,[0-9]{3})*\\.[0-9]+$/; return currencyFormat.test($currency); } $(document).on("input", "#price_meter", function() { if ($(this).val() === '' || !validateCurrency($(this).val())) { $("#price_meterError").html('لطفا قیمت متری ملک را به تومان وارد کنید.'); } else { $("#price_meterError").html(''); } }); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <label for="price_meter">قیمت متری:</label> <input type="text" placeholder="قیمت متری ملک" class="form-control divide" name="price_meter" id="price_meter"> <div id="price_meterError" class="text-danger mt-1 validate-error"></div>

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

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