简体   繁体   中英

Regex doesn't work properly

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.

Ext.form.VTypes.hyphenRe.test(v) should do the check and not allow the user to enter two - at first place.

What you suggest is to check if there is any - in field which is totally a different ball game.

The hyphenRe is somehow not being called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM