0

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

6
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by swi2
0

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? :)

0

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";
}

0

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

?>

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

Edited by Reverend Jim: Fixed formatting

0

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

Edited by Ezzaral: Snipped &quot;fake sig&quot; link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.

0

hi

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

buy

Edited by swi2: i dont right full function name

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.