How to allow only english to be entered into textbox

Started on the old forum in June 2011 · view the original in the archive · old links redirect here

#box #english #restrict #text

13 replies · 1,188 views · started 15 years ago · 1 min read

TL;DR · summary of 13 replies
  • - - - - - - - -
  • The fix for allowing only English characters (a-z, A-Z) in a textbox while replacing other languages with an empty string was not fully resolved as the discussion did not converge on a single code snippet.
  • Arpan.rnc wanted to allow only English letters and numbers but also prevent special characters like those used in Arabic or Chinese. Antony2kx suggested using regular expressions, which Arpan.rnc initially accepted but t...
  • The thread concluded with some disagreement over whether the proposed solutions met Arpan.rnc's exact requirements, with some participants suggesting additional features not requested by Arpan.rnc.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

4 participants
arpan.rnc Developer · 94 posts #1

hi wapmasters , can anybody tell me how can i allow only english test to be entered into a text box or, if a text is entered in any other language how can i replace it with a blank space or empty string ?

thanx

arpan.rnc Developer · 94 posts #3

no no .. i don't want to test it is a valid english word or not. i just want to test whether the string contains only "a-z,A-Z and other symbols" means it should not be using arabic,chinese or other such characters . What i want to block is something like this مقاطع سكس

thanx

antony2kx Developer · 94 posts #6

Your welcome a better way to do this though to prevent mix characters

$error = 0;

$word  = isset($_POST['word']) trim($_POST['word']) : '';

for($i=0; $i<=strlen($word); $i++)
  {
     if(!preg_match("/([a-zA-Z0-9_-+])/", $word[$i], $m))
     {
       break;
       $error = 1;
     }
      
  } 

if($error==1)
{
  echo "incorrect word only aphanumeric chars";
}
else
{
  echo "you have passed the test";
}
//end
arnage Lead · 811 posts #7

antony2kx wrote: Your welcome a better way to do this though to prevent mix characters

$error = 0;

$word  = isset($_POST['word']) trim($_POST['word']) : '';

for($i=0; $i<=strlen($word); $i++)
  {
     if(!preg_match("/([a-zA-Z0-9_-+])/", $word[$i], $m))
     {
       break;
       $error = 1;
     }
      
  } 

if($error==1)
{
  echo "incorrect word only aphanumeric chars";
}
else
{
  echo "you have passed the test";
}
//end

So if someone types a word in Serbian "zdravo" which means "hello" in English will pass the filter. Also, if someone writes "hello, how are you?" will not pass the filter. o.O

Try this:

$pspell_link = pspell_new("en");
$word = explode(" ", $message);
foreach($word as $k => $v) {
   if (pspell_check($pspell_link, $v)) {
      echo "spelled right";
   } else {
      echo "Sorry, wrong spelling";
   };
};
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
antony2kx Developer · 94 posts #8

If you look clearly at what help was wanted you would have seen that the code i have written is exactly what was asked for, if he wanted english statements then i could have inlcuded commas etc, but what was was asked for is that the text box should only accept a-zA-z0-9, juts look keenly before you reply.

even though zdravo is sebian its still alpha chars so it would pass,

what this code really works good on is that it helps to prevent other special chars, like russian etc.

arnage Lead · 811 posts #9

antony2kx wrote: If you look clearly at what help was wanted you would have seen that the code i have written is exactly what was asked for, if he wanted english statements then i could have inlcuded commas etc, but what was was asked for is that the text box should only accept a-zA-z0-9, juts look keenly before you reply.

even though zdravo is sebian its still alpha chars so it would pass,

what this code really works good on is that it helps to prevent other special chars, like russian etc.

Me? Do u understand english well? Here is what he asked:

"hi wapmasters , can anybody tell me how can i allow only english test to be entered into a text box or, if a text is entered in any other language how can i replace it with a blank space or empty string ?

thanx"

He did not asked for that what u have posted. Period.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
kei_ki7 Senior · 431 posts #10
function validChar($string){
$string = preg_replace("/[^A-Za-z0-9.,!@#$%^&*()-=_+]/","",$string);
return $string;
}

Did I help you? You can help me too Your donations will help me finance my studies.

arnage Lead · 811 posts #12

antony2kx wrote: dude the guy ment a-z 0-9 only so just relax, cant you see he said thanks that it worked for him, why are you cursing, dont brag, if i never knew english i owuld not be doing my masters now in computer engineering science and i am just 20; lol

This is what he ment with "only in english":

$pspell_link = pspell_new("en"); 
$word = explode(" ", $message); 
foreach($word as $k => $v) { 
   if (pspell_check($pspell_link, $v)) { 
      echo "spelled right"; 
   } else { 
      echo "Sorry, wrong spelling"; 
   }; 
}; 

You, please, leave me alone, don't explain anything more. I do not want to discuss with you on this issue, but it seems that you just want to argue. Cheers.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
arnage Lead · 811 posts #14

U r welcome bro.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close