vb.net - Want to create function validating textbox with letters only -
and function validate specific format " ad12-xyx- 123-efd-20"
i have created function numbers only' want similar this
private sub allownumericonly(byval ctrl control) if not isnumeric(ctrl.text) , ctrl.test<> "" msgbox("please enter numbers only." , vbinformation) ctrl.text = "" ctrl.focus() end if end sub please help!
here's method detect letters there far more problem that.
private function isallalpha(text string) boolean return text.all(function(ch) char.isletter(ch)) end function notice have written function rather sub. original method poorly written. should doing handling validating event of control, validating control contents in event handler , setting e.cancel true if fails, in case control retain focus. validation can done in place or method takes in string , returns boolean. example:
private sub textbox1_validating(sender object, e system.componentmodel.canceleventargs) handles textbox1.validating e.cancel = not validatetextbox(textbox1, addressof isallalphaorspace, "please enter letters or spaces.") end sub private sub textbox2_validating(sender object, e system.componentmodel.canceleventargs) handles textbox2.validating e.cancel = not validatetextbox(textbox2, addressof isallnumeric, "please enter numeric digits.") end sub private function validatetextbox(textbox textbox, validator func(of string, boolean), errormessage string) boolean dim isvalid = validator(textbox.text) if not isvalid textbox.selectall() textbox.hideselection = false messagebox.show(errormessage, "invalid input", messageboxbuttons.ok, messageboxicon.error) textbox.hideselection = true end if return isvalid end function private function isallalphaorspace(text string) boolean return text.all(function(ch) char.isletter(ch) orelse ch = " "c) end function private function isallnumeric(text string) boolean return text.all(function(ch) char.isdigit(ch)) end function private function isallalphanumeric(text string) boolean return text.all(function(ch) char.isletterordigit(ch)) end function if want validate complex format indicated might consider using maskedtextbox or else use regex. i'm not of expert in finding out how should easy letters , digits.
Comments
Post a Comment