I am trying to implement a regular expression to allow only one or two digits after a hyphen '-' and it doesn’t work properly. It allows as many digits as user types after '-'
Please suggest my ExtJS
Ext.apply(Ext.form.VTypes, {
hyphenText: "Number and hyphen",
hyphenMask: /[\d\-]/,
hyphenRe: /^\d+-\d{1,2}$/,
hyphen: function(v){
return Ext.form.VTypes.hyphenRe.test(v);
}
});
//Input Field for Issue no
var <portlet:namespace/>issueNoField = new Ext.form.TextField({
fieldLabel: 'Issue No',
width: 120,
valueField:'IssNo',
vtype: 'hyphen'
});
This works only to the limit that it allows digits and -. But it also has to allow only 1 to 2 digits after - at most.
Is something wrong in my regex? hyphenRe: /^\d+-\d{1,2}$/,
It could be that it expects the input to match exactly with the Regex pattern you have specified. This site has a zip code example that also limits the numbers after the hyphen.
It uses an Else statement to let form function accept one pattern or another pattern option.
The Regex used for the 5-4 format is: “^\d{5}-\d{4}$”
Hope this helps a bit.