PDA

View Full Version : can javascript validate correct entry in a field as well as it can accept empty field



maaz4
Feb 9th, 2011, 08:36 AM
hiiii,
im new to javascripts . Can anyone help me to know that a javascript which is validating a phone number accepts only digits but if the text field is left empty it should accept the entry as an empty entry....


plz share knowledge u will be more knowledgeable :)

Philip M
Feb 9th, 2011, 09:45 AM
Here you are:-


PHONE <input type= "text" id = "phone" onblur = "chkPhone()">

<script type = "text/javascript">

function chkPhone() {

var ph = document.getElementById("phone").value;
if (ph !="") {
if (/\D/g.test(ph)) {
alert ("Phone number may contain only digits");
document.getElementById("phone").value = "";
setTimeout("document.getElementById('phone').focus()", 25);
}
}

}

</script>

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki -Japanese Zen priest, ?-1971)

maaz4
Feb 9th, 2011, 10:01 AM
million thanx to u friend :)

the saying is too good :)

hope ur knowledge increases day by day :)