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

Dani AI

Generated

The original test is checking for "any non-digit" instead of "only letters", so a mixed string like keval123 still passes. The old eregi approach used in the first post is deprecated/removed in modern PHP — switch to a PCRE-based validation and trim the input first.

A simple, Unicode-safe server-side check:

function validate_alpha($val) {
    $val = trim($val);
    if ($val === '') return false;
    return (bool) preg_match('/^\p{L}+$/u', $val);
}

This matches only Unicode letters (so accented characters count). If real names should allow spaces, hyphens or apostrophes (O'Connor, Jean-Luc, María José), use a slightly broader pattern and then enforce that the string starts and ends with a letter:

return (bool) preg_match("/^\p{L}[\p{L}\p{M}'\-\s]*\p{L}$/u", $val);

Troubleshooting and tips: trim input and check for empty strings; cast preg_match to boolean; use mb_strlen if you need multibyte-aware length checks. Watch for simple mistakes like the variable-name typo in 's first example ($var vs $val). 's loop-based test works but is less efficient and harder to maintain. 's client-side regex is useful for UX, but never rely on client-side checks alone — always validate on the server. If using C-type functions for performance, be aware they are byte/locale-dependent and may not accept non-ASCII letters; for international names prefer the PCRE Unicode approach above.

Recommended Answers

All 7 Replies

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.