jquery masked input plugin to not clear field when errored

后端 未结 7 794
难免孤独
难免孤独 2020-12-29 20:28

I\'m looking at the http://digitalbush.com/projects/masked-input-plugin/

I\'m calling it like this:

$(control).mask(\'999-999-9999\');
相关标签:
7条回答
  • 2020-12-29 20:43

    Looking for into the pluging script the unmask method.

    $('#checkbox').unmask();
    
    0 讨论(0)
  • 2020-12-29 20:49

    Adding Placeholder could solve the problem.

    $(control).mask('999-999-9999');
    

    Add an empty place holder into mask. see below

    $(control).mask('999-999-9999', { placeholder: "" });
    

    which would replace _ on the input text field by default. so there would bot be any _ left if the input length is dynamic and not fixed.

    0 讨论(0)
  • 2020-12-29 20:56

    Set autoclear option to false.

    $(control).mask('999-999-9999', {autoclear: false});
    
    0 讨论(0)
  • 2020-12-29 20:56

    In addition to removing the input.val("") in checkVal() you can also change the call to clearBuffer. In the original code it is: clearBuffer(0, len); removing all user input. if you change this to clearBuffer(lastMatch + 1, len); the user input will be displayed, followed by the mask placeholders that are still needed to complete correct input.

    I have also added a user message in the .bind. This works for us, as we are using the MaskedInput for exactly one type of input. I'm checking for any input going further than position 7, because that's where the user input starts.

    Here is what I did:

    .bind("blur.mask", function() {
        // find out at which position the checkVal took place
        var pos = checkVal();
        // if there was no input, ignore
        if (pos <=7) {input.val(""); clearBuffer(0, len);}  
        // if the user started to input something, which is not complete, issue an alert
        if (pos > 7 && pos < partialPosition) alert("Tell the user what he needs to do.");
        if (input.val() != focusText)
        input.change();
    })
    
    0 讨论(0)
  • 2020-12-29 21:02

    Try update file jquery.maskedinput.js

    In function function checkVal(allow) set parameter allow on true. Its help for me.

     function checkVal(allow) {
                        allow = true; ///add this command
    //..............
    }
    
    0 讨论(0)
  • 2020-12-29 21:03

    You should delete statement input.val(""); in checkVal() function for a proper solution.

    If you're using minified version, you should search and delete statement:

    if(!a&&c+1<i)f.val(""),t(0,k);else
    
    0 讨论(0)
提交回复
热议问题