How to restrict a field to accept first 2 char. as alphabets and last 5 numbers

KS13
Tera Contributor

Yesterday, a solution was provided for the same question, but after the error message was closed, it still allowed the user to submit with the inaccurate format. 

 

Have written the following script and still not working:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var rexp = '/^[a-zA-Z]{2}[0-9]{5}]$/';   
var dataValue=g_form.getValue('provide_id');

if (dataValue.match(rexp)==null){
alert('The new  ID format does match the ID format.');
g_form.clearValue('provide_id');
g_form.showFieldMsg('provide_id', 'The new  ID format does match the ID format.', 'info', true);
} else {
g_form.clearMessages();
}
}

1 ACCEPTED SOLUTION

Community Alums
Not applicable

What is the issue ? I havent adjusted is fully for your use case - you can check if the first input keystroke is alphabetic, if second is alphabetic, is third is numeric, etc.

Try this (I tested it on my PDI for several use cases - allows input if, first & second input is character, 3-5 are numbers. If > 7 - error)

function onLoad() {

var control = g_form.getControl('provide_id');
Event.observe(control, 'input', function(e) {

var oldInputVal = e.target.defaultValue;
var newInputVal = e.target.value;
var regexMandatoryChars = /^[a-zA-Z]{2}/gm;
var regexLastChars = /\d{5}$/gm;
var regex1Char = /[a-zA-Z]$/gm;
var regex2Char = /[a-zA-Z]$/gm;
var regex3Char = /\d$/gm;
var regex4Char = /\d/gm;
var regex5Char = /\d/gm;
var regex6Char = /\d/gm;
var regex7Char = /\d/gm;

var oldInput = (function () {
var i = oldInputVal;

return {
get: function () {
return i;
},
set: function (val) {

i = val;
}
};
})();

var userVal = newInputVal;///.replace(regexSpecialChars, "");

if(userVal.length == 1){
if(!regex1Char.test(userVal)){
alert("First two characters must be alphabetical! You typed " + userVal + ", type an alphabetical character instead!");
userVal = userVal.replace(/.$/, "");
}
} else if(userVal.length == 2){

if(!regex2Char.test(userVal)){
alert("First two characters must be alphabetical! You typed " + userVal + ", type an alphabetical character as a second one instead!");
userVal = userVal.replace(/.$/, "");
}

} else if(userVal.length == 3){
if(!regex3Char.test(userVal)){
alert("Last 5 characters must be numeric. " + userVal + " is not a number!");
userVal = userVal.replace(/.$/, "");
}
} if(userVal.length == 4){
if(!regex3Char.test(userVal)){
alert("Last 5 characters must be numeric. " + userVal + " is not a number!");
userVal = userVal.replace(/.$/, "");
}
} if(userVal.length == 5){
if(!regex3Char.test(userVal)){
alert("Last 5 characters must be numeric. " + userVal + " is not a number!");
userVal = userVal.replace(/.$/, "");
}
} if(userVal.length == 6){
if(!regex3Char.test(userVal)){
alert("Last 5 characters must be numeric. " + userVal + " is not a number!");
userVal = userVal.replace(/.$/, "");
}
} if(userVal.length == 7){
if(!regex3Char.test(userVal)){
alert("Last 5 characters must be numeric. " + userVal + " is not a number!");
userVal = userVal.replace(/.$/, "");
}
} if(userVal.length > 7){
userVal = userVal.replace(/.$/, "");
alert("Allowed lenght [2 characters and 5 numbers] reached. No more characters allowed!");
}
oldInput.set(userVal);
g_form.setValue('provide_id', oldInput.get());


});

}

 

View solution in original post

12 REPLIES 12

Brad Bowman
Kilo Patron
Kilo Patron

You are clearing the value of the field after the alert - is this working?  Is the field mandatory?

Hi Brad,

Thank you for responding, The field is mandatory. Yes, after the user clicks ok on the pop error message, the field then clears previous values. 

I'm confused how the user can submit an incorrect format after closing the error message then.  Are you saying the regex / validation is not working - so they don't get the error message when they should?

You are right Brad. I think regex/validation is not working.