Reputation: 19327
I set formatting mask on a textfield :
$(document).ready(function() {
$("#budget_para_prevision").inputmask("9999999999999.99"); // 13 digits before "."
});
The problem happens after posting the form when the length of the digits before the "." sign is less than 13 then the formatting characters are written automatically with the $_POST
variable , it gives something like this :
391000000000_.__
So how to remove the _
and the .
sign in this case ?
Upvotes: 0
Views: 108
Reputation: 91744
I guess it would depend on what jQuery inputmask plugin you are using exactly, but if it is Robin Herbots plugin, you can make parts of your input optional and specify lengths:
$(document).ready(function() {
$("#budget_para_prevision").inputmask("9{1,13}[.99]"); // 13 digits before "."
});
Of course you could also fix it at the backend with rtrim($input, "._")
but preventing the input in the first place would be better.
Upvotes: 1
Reputation: 48415
You can remove the unwanted characters using a combination of str_replace
and rtrim
. Something like this:
$input = "391000000000_.__";
$result = str_replace("_", "", $input); // Remove instances of underscore.
$result = rtrim($result, "."); // Remove the dot if it's the last character.
Or you can just do the whole lot with a single rtrim
:
$result = rtrim($input, "._");
Upvotes: 2
Reputation: 11245
You can have part of your mask be optional. Anything listed after '?' within the mask is considered optional user input. The common example for this is phone number + optional extension.
$(document).ready(function() {
//if 12 and more digits are optional
$("#budget_para_prevision").inputmask("999999999999?9.99");
});
From docs
Upvotes: 2