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

That regex should work, but I fought with it for a little bit and couldn't figure it out.  Here's a script that works:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
	var rexp = /^[a-zA-Z]{2}\d{5}$/;   
    var dataValue = rexp.exec(newValue);

    if (dataValue === 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();
    }
}

Thank you Brad, this worked perfectly! Much appreciated. 

Glad to hear.  You are welcome!

Community Alums
Not applicable

So, forst of all, you need to know the exact number of characters the user is typing. So, if you have allowed total lenght of 10 chars, you need to check if the first 2 are letters + whatever chars here (3 chars in total) + 5 number chars.

This means you need to check this somehow. I had similar requirement in the past and ended with onLoad client script, but my solution was to check for some forbiden characters (but still kep the already typed in input). I will send a link to the solution, but it must be changed a bit to handle your needs. Will try this in a moment. In the mean time - here is the link:
https://www.servicenow.com/community/now-platform-articles/escape-special-characters-without-loosing...

Community Alums
Not applicable

Try something like this (tested it quite fast so you have to test it yourself)

function onLoad() {

var control = g_form.getControl('description');
Event.observe(control, 'input', function(e) {
	
	var oldInputVal = e.target.defaultValue;
	var newInputVal = e.target.value;
	//var regexSpecialChars = /\/|\\|\"|;|<|>|\||\t|~|#|\*|:|\?|{|}|\s/gm;
	var regexMandatoryChars = /[a-zA-Z]{2}.{3}\d{5}/gm; //if you need only 2 chars + 5 digits - remove the .{3} part of teh regex

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

		return {
			get: function () {
				return i;
			},
			set: function (val) {
				
				i = val;
			}
		};
	})();
	
	var userVal = newInputVal;//.replace(regexSpecialChars, "");
	if(newInputVal.length == 10){
	if(!regexMandatoryChars.test(userVal)){
		alert("NOPE " + userVal);
		
	} 
	} else if(newInputVal.length > 10){
		alert("No more chars allowed!")
		userVal = userVal.replace(/.$/, "");
	}
	oldInput.set(userVal);		
	g_form.setValue('description', oldInput.get());
	

});

}