I am a newbie in JavaScript. Can any one explain me how to do a numeric validation (I want only numbers as an input) to textboxes? And how I can do the same only for characters (A-Z, a-z).
<script>
function validNum(inField){
if (isNaN(inField.value)==true){
alert ("Please enter a valid number"
inField.focus()
inField.select()
}
}
</script>
As for the charachter function, do you want to allow numbers and characters, or just characters? Get the Best Answers! faq333-2924
Happy 2003!
mikewolf@tst-us.com
<script>
function alphaOnly(inField){
// declare var of not valid characters thanks to onpnt for the list and the knowledge
var illChar = /[~`!@#$%^&*()_+=:;"'{}\[\]][0-9]/gi;
if (inField.value.search(illChar) != -1){
alert "Please enter characters only!"
inField.focus()
inField.select()
}
}
also congrat's on tipmaster of the week ---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee"alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924
Thanks! I took advantage of everyone else being on vacation for the New Year! Get the Best Answers! faq333-2924
"A witty saying proves nothing." - Voltaire
mikewolf@tst-us.com
I tried to put the two functions in that order and to assign them to the proper textboxes, unfortunately everything stop working. I am after all a newbie. Can someone explain me why?
<script>
function validNum(inField){
if (isNaN(inField.value)==true){
alert ("Please enter a valid number"
inField.focus()
inField.select()
}
}
function alphaOnly(inField){
// declare var of not valid characters thanks to onpnt for the list and the knowledge
var illChar = /[~`!@#$%^&*()_+=:;"'{}\[\]][0-9]/gi;
if (inField.value.search(illChar) != -1){
alert "Please enter characters only!"
inField.focus()
inField.select()
}
}
</script>
Html assignes:
<input name="alphaOnlyText" onBlur="alphaOnly(this)">
<input name="numbersOnly" onBlur="validNum(this)">
<script>
function validNum(inField){
if (isNaN(inField.value)==true){
alert ("Please enter a valid number"
inField.focus()
inField.select()
}
}
function alphaOnly(inField){
var illChar = /[~`!@#$%^&*()_+=0123456789:;"'{}\[\]]/gi;
if (inField.value.search(illChar) != -1){
alert ("Please enter characters only!"
inField.focus()
inField.select()
}
}
</script>
Html assignes:
<input name="alphaOnlyText" onBlur="alphaOnly(this)">
<input name="numbersOnly" onBlur="validNum(this)"> Get the Best Answers! faq333-2924
"A witty saying proves nothing." - Voltaire
mikewolf@tst-us.com
NOTE: inField.select() will not be valid since there would no longer be anything to "select" Get the Best Answers! faq333-2924
"A witty saying proves nothing." - Voltaire
mikewolf@tst-us.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.