Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text/numeric validation

Status
Not open for further replies.

ashz

Programmer
Dec 9, 2002
43
0
0
ES
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).

Thanks a lot.

 
<script>
function validNum(inField){
if (isNaN(inField.value)==true){
alert (&quot;Please enter a valid number&quot;)
inField.focus()
inField.select()
}
}
</script>

<input name=&quot;numbersOnly&quot; onBlur=&quot;validNum(this)&quot;>


As for the charachter function, do you want to allow numbers and characters, or just characters? Get the Best Answers! faq333-2924
Happy 2003! [cheers]
mikewolf@tst-us.com
 
Thanks for the script, the other function that I am looking for is just for characters.
 
<script>
function alphaOnly(inField){
// declare var of not valid characters thanks to onpnt for the list and the knowledge
var illChar = /[~`!@#$%^&*()_+=:;&quot;'{}\[\]][0-9]/gi;
if (inField.value.search(illChar) != -1){
alert &quot;Please enter characters only!&quot;
inField.focus()
inField.select()
}
}

</script>

<input name=&quot;alphaOnlyText&quot; onBlur=&quot;alphaOnly(this)&quot;>

Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Hey Mike, [thumbsup2]

also congrat's on tipmaster of the week [medal] ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);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
&quot;A witty saying proves nothing.&quot; - 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 (&quot;Please enter a valid number&quot;)
inField.focus()
inField.select()
}
}

function alphaOnly(inField){
// declare var of not valid characters thanks to onpnt for the list and the knowledge
var illChar = /[~`!@#$%^&*()_+=:;&quot;'{}\[\]][0-9]/gi;
if (inField.value.search(illChar) != -1){
alert &quot;Please enter characters only!&quot;
inField.focus()
inField.select()
}
}
</script>

Html assignes:
<input name=&quot;alphaOnlyText&quot; onBlur=&quot;alphaOnly(this)&quot;>
<input name=&quot;numbersOnly&quot; onBlur=&quot;validNum(this)&quot;>
 
<script>
function validNum(inField){
if (isNaN(inField.value)==true){
alert (&quot;Please enter a valid number&quot;)
inField.focus()
inField.select()
}
}

function alphaOnly(inField){
var illChar = /[~`!@#$%^&*()_+=0123456789:;&quot;'{}\[\]]/gi;

if (inField.value.search(illChar) != -1){
alert (&quot;Please enter characters only!&quot;)
inField.focus()
inField.select()
}
}
</script>

Html assignes:
<input name=&quot;alphaOnlyText&quot; onBlur=&quot;alphaOnly(this)&quot;>
<input name=&quot;numbersOnly&quot; onBlur=&quot;validNum(this)&quot;> Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Thank a lot, it works, I have another small question.

How can I also clear (empty) the content of the field if it is not a valid input?
 
using the code I wrote above you would say:

inField.value = &quot;&quot;
inField.focus()

NOTE: inField.select() will not be valid since there would no longer be anything to &quot;select&quot; Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top