javascript - Textbox validations and color change -
i new on java-script.
i have developed system using vs asp.net 4.0 using c#, have more 45 web forms want add validations using java-script. have following code letters , numbers validations. see code below. code doesn't work way want because can validate when press space bar.
function numeralsonly(evt) { evt = (evt) ? evt : event; var charcode = (evt.charcode) ? evt.charcode : ((evt.keycode) ? evt.keycode : ((evt.which) ? evt.which : 0)); if (charcode > 31 && (charcode < 48 || charcode > 57)) { alert("enter numbers in field."); return false; } return true; } function lettersonly(evt) { evt = (evt) ? evt : event; var charcode = (evt.charcode) ? evt.charcode : ((evt.keycode) ? evt.keycode : ((evt.which) ? evt.which : 0)); if (charcode > 31 && (charcode < 65 || charcode > 90) && (charcode < 97 || charcode > 122)) { alert("enter letters only."); return false; } return true; }
can please me following;
1.validate numbers , change color of text box red.
2.validate letters only , change color of text box red.
- validate invalid email address , change color of text box red.
4.validate range of date. e.g if he/she enters invalid date 05/05/2001 ,i need current date.
5.highlight text box red required text box.
for question 5 : call method in onchange or onblur method.it validate, when left field.if want validation on clicking on of key call onkeyup,onkeydown.
function validations() { var textbox = document.getelementbyid("textbox1"); if (textbox.value == "") { textbox.style.bordercolor = "red"; } }
for question 1:
function namevalidation(){ var nametb = document.getelementsbyname("tb1"); var nameregex = new regexp("^[a-za-z]+$"); var namevalue = nametb.value.trim(); if ((!nameregex.test(namevalue))) { nametb.style.border = '1px solid red'; } else { nametb.style.border = "none"; }
}
Comments
Post a Comment