[Javascript] Validation
Rodney Myers
rodney at aflyingstart.net
Wed Jun 20 12:12:10 CDT 2001
Here is some code that ensures that input is permitted characters only -
in this case digits and a point :
function dfix(el){
el.value=decimals(el.value);
}
function decimals(str){ return(clean(str,"1234567890.")) }
function clean(str,permitted){
str=""+str;
var nchar,n;var out="";
for(n=0;n<str.length;n++)
{
nchar=str.charAt(n);
out+=permitted.indexOf(nchar)>-1?nchar:"";
}
return(out);
}
This code does not verify by an alert but gets in and chucks out the
unwanted characters.
Admittedly this is not always what is wanted, but sometimes it is
appropriate.
<input .... onchange="dfix(this)">
We pass the input object with 'this' and so can set its value
el.value=decimals(el.value);
For A-Za-z we could have
<input .... onchange="azfix(this)">
function azfix(el){
el.value=azAZ(el.value);
}
function azAZ(str){
var permitted="abcdefghijklmnopqrstuvwxyz";
permitted+=permitted.toUpperCase();
return(clean(str,permitted)) }
Noting that the above function clean() is re-used.
hth
Rodney
Mark.Benton at egg.com wrote:
> Can someone tell me or provide some sample code of a function to
> validate that a text box entry is apha charcters only.
>
> Thanks in advance.
>
--
Shop at ssistant Add-ons and Developer Workshops
http://www.aflyingstart.net/addons/
Enquiries regarding Shop at ssistant Classic training :
Call 01256 880770
Rodney Myers
Based in Oxford, England
Technical Director, Shop at ssistant eCommerce Solutions
More information about the Javascript
mailing list