ASP.NET Client Side TextBox Validation
Like most programming tasks, client validation can be as simple or as more complex . Consider Employee Payroll Form , I am having Emp name, Salary, Address ,etc.If any user trying to enter the characters into salary (Textbox) mean’s, It does'nt accepts the characters and it will show alert message and if trying to enter numbers into Emp Name Field also it will raise alert messages respectively.
The steps is as follow:
-
Open New Website, -
Drag two Textboxes from toolbox (Web controls) and one asp.met Button control. -
Wite the following code in html (Inlinecode).
Then in the CodeBehind File add the following procedure.
Then call this procedures (Numbers ,test) In the Page Load Event.' attach the "number" javascript function to the textbox Public Sub numbers(ByVal txtbox As System.Web.UI.WebControls.TextBox) txtbox.Attributes.Add("onkeypress", "return clickButton(event)") End Sub' attach the "test" javascript function to the textbox Public Sub test(ByVal txtbox1 As System.Web.UI.WebControls.TextBox) txtbox1.Attributes.Add("onkeypress", "return clickButton1(event)") End Sub
In this way we are attaching our function to the onkeypress event of the textbox that will fire when user will press any key in these textboxes.Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' here "salary" is the id of the textbox, same applies to all procedures numbers(salary) numbers(mobileno) numbers(homephone) test(EmpName) test(Address) End Sub
Surely this example will help in client side validation.
Happy coding.
by Majith
No comments:
Post a Comment