c# - Regex for two characters -


how construct regex allow enter ca or ch? tried \bc(a|h) , c(a|h) need validate in keypress event of textbox this;

private regex _regex = new regex(@"c(a|h)");  private void txtcach_keypress(object sender, keypresseventargs e) {     if (char.iscontrol(e.keychar))         return;      if (!_rolfregex.ismatch(e.keychar.tostring().toupper()))         e.handled = true; } 

you can use

if (e.keychar != (char)8)  // not backspace key    if (!regex.ismatch(txtcach.text.toupper() + e.keychar.tostring().toupper(), @"^c[ah]?$"))  // if value not ch or ca        e.handled = true;   // not let pass 

inside keypress event handler, txtcach.text contains value before adding next key. so, full value need add newly pressed key value. after that, can check if value 1 can accept.

^c[ah]?$ 

this regex accepts c or ca or ch values, can type them in.

then, need validate @ other event ^c[ah]$ (leave event, example).

live validation cannot performed @ same time final validation.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -