# Web Development > JavaScript JavaScript form address field validations

## ashaflint

In javascript form validations, how to allow numbers, special characters, alphabets in address field? the address field doesn't allow only numbers or alphabets or characters. it allows all the three otherwise it shows the alert message. 

if anybody konw the answer please explain me.

----------


## psuresh1982

Hi ashaflint,

    Look at the following code..

function checkAddress(sText)
{
var ValidChars = "0123456789"; 
// Here what ever you want to put. As you mentioned numbers or alphabets or characters.
      var IsNumber=true;
  	  var Char;
  		 for (i = 0; i < sText.length && IsNumber == true; i++) 
      		{ 
      			Char = sText.charAt(i); 
      			if (ValidChars.indexOf(Char) == -1) 
        		             {
        			 IsNumber = false;
       			  }
     	             }
   		return IsNumber;
}

In the above function sText is the value of the address field. I think it will help to you.

------------------------
suresh

----------


## psuresh1982

Hi ashaflint,

I think it is easy to give the alert message when the user enters only numbers or alphabets or characters...Look at the following script, now i give the alert message when the user enters only numbers.

function checkAddress(sText)
{
var ok = "0123456789"; 
var f=true;
var Char;
for (i = 0; i < sText.length && f == true; i++) 
{ 
Char = sText.charAt(i); 
if (ok.indexOf(Char) == -1) 
{
f = false;
}
}

if (f==true)
{
 alert("user enters only numbers");
}
}

So you can change this script to alphabets or characters also. The only thing is you should change the value of "ok". In the above function i give only numbers. I hope this will help to you.

-----------------------
suresh

----------


## samineni6

hi

the code given by suresh is correct but little modification then it will work

function checkAddress(sText)
   { 
var ok = "0123456789"; 
var f=true;
var Char;
var text=sText.value;
for (i = 0; i 
{ 
 Char=text.charAt(i); 
 if(ok.indexOf(Char) == -1) 
{
f = false;
}

}
 if (f==false)
{
alert("user enters only numbers");
}
}

----------


## samineni6

sorry u copy this one

function checkAddress(sText)
   { 
var ok = "0123456789"; 
var f=true;
var Char;
var text=sText.value;
for (i = 0; i 
{ 
 Char=text.charAt(i); 
 if(ok.indexOf(Char) == -1) 
{
f = false;
}

}
 if (f==false)
{
alert("user enters only numbers");
}
}

----------


## samineni6

function checkAddress(sText)
   { 
var ok = "0123456789"; 
var f=true;
var Char;
var text=sText.value;
for (i = 0;i 
{ 
 Char=text.charAt(i); 
 if(ok.indexOf(Char) == -1) 
{
f = false;
}

}
 if (f==false)
{
alert("user enters only numbers");
}
}

----------


## samineni6

getting problem while displaying for loop 

for (i = 0;i

----------


## sarithahere528

function checkAddress(sText)
{
var ok = "0123456789";
var f=true;
var Char;
for (var i = 0;i
{
Char=sText.charAt(i);
if(ok.indexOf(Char) == -1)
{
f = false;
}

}
if (f==false)
{
alert("user enters only numbers");
}
} :Smile:

----------


## nagasen

in onkeypress function you can check this one like this

function onlyno(){
    key=key.event();

if(key.indexOf("0123456789")>-1){
 return true
else return false
}

}

----------

