验证表单输入,如果错误则显示弹出窗口

时间:2016-02-04 12:50:22

标签: javascript

我有一个在提交表单时触发的JavaScript函数。我想检查价格格式并验证最小值和最大值之间的价格。如何显示弹出窗口以通知用户他的输入格式错误?

function priceSetup(){

       var minPrice=document.getElementById("minPrice").value;
       var maxPrice=document.getElementById("maxPrice").value;
/// add checking here and show popup if something wrong???

    document.getElementById("priceForm").action="<?php echo base_url().MY_PATH; ?>getCategory/".concat(minPrice).concat("/"). concat(maxPrice);
    document.getElementById("priceForm").submit();
}

2 个答案:

答案 0 :(得分:0)

var price = new RegExp(/^\$?[\d,]+(\.\d*)?$/);

if(price.test(minPrice) && 
   price.test(maxPrice) && 
   ParseInt(maxPrice,10) -   
   ParseInt(minPrice,10) > 0)
{
  //popup
} 
else
{
    document.getElementById("priceForm").action="<?php echo base_url().MY_PATH; ?>getCategory/".concat(minPrice).concat("/"). concat(maxPrice);
    document.getElementById("priceForm").submit();
}

答案 1 :(得分:0)

一种方式是添加事件git mv -k * ./subDir,如下所示:

onSubmit()

之后修改你的javascript如下:

<form id="priceForm" action="" onSubmit="return priceSetup()">
...
</form>

在函数返回function priceSetup() { var returnValue = true; var minPrice=parseInt(document.getElementById("minPrice").value); var maxPrice=parseInt(document.getElementById("maxPrice").value); if ( (parseInt(document.getElementById("Price").value) >= minPrice) && (parseInt(document.getElementById("Price").value) <= maxPrice) ) { document.getElementById("priceForm").action="<?php echo base_url().MY_PATH; ?>getCategory/".concat(minPrice).concat("/"). concat(maxPrice); } else { /*... Code for showing a message ... */ returnValue = false; } return returnValue; } 之前,将不会执行提交。