I have placed two text boxes(server side) for Name and Age in asp.net 2.0.
Their ids are txtPatientName and txtPatientAge.
The validation to be done is both text boxes should not be left empty.
I have written two javascript functions as follows.
Expand|Select|Wrap|Line Numbers
- function isName()
- {
- var str = document.forms[0].txtPatientName.value;
- if (str == "")
- {
- document.forms[0].txtPatientName.focus();
- alert("The NAME field is blank.Please enter name.")
- return true;
- }
- }
- function age()
- {
- var age=document.forms[0].txtPatientAge.value;
- var name=document.forms[0].txtPatientName.value;
- if (age== "")
- {
- alert("The age field should not be blank");
- return false;
- }
- }
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Page.IsPostBack == false)
- {
- txtPatientName.Attributes.Add("onfocusout", "javascript:isName()");
- txtPatientAge.Attributes.Add("onfocusout", "javascript:age()");
- }
What I want is:When name is not given and if we click TAB, then it should show alert as “name should not be blank” and focus should be on “name” textbox.Then it goes to age textbox.
After this, When age is not given and if we click TAB, then should show alert as “age should not be blank” and focus should be on “age” textbox.
Plz help me in this regard by sending sample code.
Thanks
Silpa