表单提交有2个不同的错误,具体取决于输入值-1个不起作用 [英] Form submit with 2 different errors depending on input value - 1 is not working

查看:57
本文介绍了表单提交有2个不同的错误,具体取决于输入值-1个不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里发布了一个有关如何使2个错误工作的问题:

I posted a question on how to make 2 errors work here: Multipile forms with input and submit button with the same action with Javascript

我需要2个错误,如果输入少于15位,则需要1个错误,第二个错误是如果输入以9900开头(如果输入,则有错误,然后将其重定向到另一个页面). 它工作了一段时间,尽管突然停止发出第二个错误(如果输入以9900开头),则页面为:

I needed 2 errors, 1 error if the input if less than 15 digits, the second error is if the input begins with 9900 (if it does, there is an error and then it redirects to a different page). It worked for a while, although suddenly it stopped giving the second error (if input starts with 9900), the page is: http://www.unlocker.co.il/shop/sim-unlock-htc-mobile-device/

每个表单都有自己的ID:unlock1,unlock2,unlock3等. JS文件包括:

Each form has it's own id: unlock1, unlock2, unlock3 etc. the JS files include:

    jQuery(function($){
$('form#unlock1').on('submit', function (e){
    if($('form#unlock1 > input.the_imei').val().length == 15){
        if($(this).val().indexOf('9900') === 0){
         alert('לפי המספר IMEI, ברשותכם מכשיר CDMA, אנא ראו מידע נוסף בעמוד פתיחת מכשירי CDMA');
         window.location = 'http://www.unlocker.co.il/sim-unlock-cdma-mobile-device';
         e.preventDefault();
             }
        return;
    }

alert('אנא מלאו מספר IMEI בעל 15 ספרות');
e.preventDefault();
});   
})

和表格是:

<form id="unlock1" class="cart" enctype="multipart/form-data" method="post" name="unlock"> <input class="the_imei" style="width: 80%; border-radius: 15px;" name="the_imei" type="text" value="" placeholder="מספר סידורי IMEI של המכשיר (חייג #06#*)" /> <input class="add-to-cart" name="add-to-cart" type="hidden" value="39" /> <button class="unlockButton" type="submit" value="submit">פתח לכל הרשתות בישראל </button> </form>

每个表单都有一个不同的ID(例如Unlock1),并且在它自己的JS文件中,ID像在2个地方列出的一样.

Each form has a different id (example Unlock1) and on it's own JS file the id like listed in 2 places.

我不知道为什么最少15位数字"错误起作用,但是如果输入以9900开头"错误不再起作用.

I can't figure out why the "minimum 15 digits" error works, but the "if input begins with 9900" error does not work anymore.

谢谢!

推荐答案

这就是我要这样做的方式.尽量避免使用过多的嵌套if语句.当用户首先输入不正确的值时,请使用return语句创建转义子句.

This is how I would do this. Try to avoid using too many nested if statments. Instead create the escape clauses with return statements when the user enters the incorrect value first.

$('form#unlock1').on('submit', function(){
  var $el = $('form#unlock1 > input.the_imei');

  if($el.val().length<15){
    //Not enough characters
    return false;
  }

  if($el.val().substring(0,4)=='9900') {
    //Value begins with 9900
    return false;
  }

  //User has entered a correct imei
});

这篇关于表单提交有2个不同的错误,具体取决于输入值-1个不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆