Hi,

I want to make a php validation for textbox which limit user to enter only character.

i using following code

function validate_alpha($val)
		{
		
			return eregi("[^0-9]",$val);
		}

if i type 123 it will give error. that is ok.

but when i type keval123 it doesn't produce error.

how to solve this problem.

-keval

Just one character? So "a" or "3", but not "ab" or "3r" etc?

If so, just do:

if( strlen($val) == 1 ) {
 // Fine
}
else {
 // Error
}

Or do you mean something else? :)

Use ctype_alnum() function to check the input data. I give you an example code:

function validate_alpha($val)
{
if(ctype_alnum($var))
return "string is accepted.";
else
return "string is not accepted";
}

hi, navdeep7489

Your code is not working.

-keval

Hello Keval,
Check this code:

<?php
function validate_alpha($val)
{
if((ctype_alnum($val)) && (!is_numeric($val[0])))
return "string is accepted.";
else
return "string is not accepted";
}
echo validate_alpha("1navdeep"); //not accepted
echo validate_alpha("navdeep"); // accepted
echo "\n".validate_alpha("nat#$#132"); // not accepted

?>

function checkString($string){
for($i=0; $i<10; $i++)
    if(strpos($string,"$i") > -1)
         return false;
return true;
}

To validate Name i think you might require alphabates as string
Call this function where you want to validate.

function isAlphabet(elem, helperMsg)//elem => var fname=document.getElementById('fname')
{
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

Or you can visit the link
Hope this may useful to you.
Thanks & Regards
Chintan

hi

keval_hack i think using ctype_alphanum() function which is avaliable in php will solve your problem

buy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.