Hello @rohitatwp
The regular expressions you enter through the fields’ settings are used to validate the fields’ values, not to format them.
I’ll try to describe the process of using a regular expression to format a field.
Assuming you have the fieldname1 field where you want to accept values with nine characters like:
XXX-XXX-XXX
The separator can be a hyphen or dot symbol or no separator at all.
But no matter the format of the value entered by the user, you want to format it as XXX-XXX-XXX.
The regular expression to validate the value entered by the user would be:
/^[a-x0-9]{3}[\-\.]?[a-x0-9]{3}[\-\.]?[a-x0-9]{3}$/i
But now, if you want to format the field’s value dynamically, you must insert an “HTML Content” field in the form with a piece of code similar to:
<script>
jQuery(document).on('change', '[id*="fieldname1_"]', function(){
var v = new String(this.value);
if(/^[a-x0-9]{3}[\-\.]?[a-x0-9]{3}[\-\.]?[a-x0-9]{3}$/i.test(v))
{
this.value = v.replace(/^([a-x0-9]{3})[\-\.]?([a-x0-9]{3})[\-\.]?([a-x0-9]{3})$/i, "$1-$2-$3");
}
});
</script>
If you need a custom coding service to implement your project, you can contact me through my private website: Click Here
Best regards.
Hello @codepeople
I did follow your above Regular Expression. In fact, I customized it a bit as follows :
/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i
thereby allowing a SPACE as well along with the HYPHEN or a DOT
and all that works fine.
However, the HTML Content field as suggested by you doesn’t seem to work. So I further tweaked it a bit to following :
<script>
jQuery(document).on('change', '[id*="fieldname6_"]', function(){
var v = new String(this.value);
if(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i.test(v))
{
this.value = v.replace(/^[A-Z]{5}[\-]?[0-9]{4}[\-]?[A-Z]{1}$/i, "$1-$2-$3");
}
});
</script>
since I wanted to dynamically capitalize the characters as well as add the HYPHEN alone. But that doesn’t seem to work. Earlier, I had forgotten to change the fieldname1 to my fieldname6 but even after doing that it did not work. I think the HTML Content field is the problem.
Requesting you to kindly help please.
Hello @rohitatwp
No, I’m sorry, you are not using the regular expression for replacement as you should.
If you check my hypothetical example, the regular expression for replacement is a little different from the regular expression I used to validate the value. I’ve enclosed between parentheses the components to use with the special vars $1, $2, and $3
Best regards.
Sorry @codepeople
I made a mistake in copying the code from the mail and therefore the double quotes (“) got replaced with (").
I fixed that. And it has begun working but the problem is after replacement instead of giving the Capitalized and Hyphenated String it is dynamically returning “$1-$2-$3” for anything that I enter there.
My HTML Content Code is :
<script>
jQuery(document).on('change', '[id*="fieldname6_"]', function(){
var v = new String(this.value);
if(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i.test(v))
{
this.value = v.replace(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i, "$1-$2-$3");
}
});
</script>
What’s wrong now? Can you kindly help?
Thanks
Hello @rohitatwp
As I told you in the previous entry, please, look at the parentheses use in my code. If you don’t use parentheses, the replace method cannot identify the constants $1, $2, and $3 to use for replacement.
This was my code:
v.replace(/^([a-x0-9]{3})[\-\.]?([a-x0-9]{3})[\-\.]?([a-x0-9]{3})$/i, "$1-$2-$3");
This question is not specific to our plugin. Please, I recommend you to read any of the regular expressing tutorials available for free on the Internet.
Best regards.