form validation allow only english alphabet characters

I’d like to restrict my form input from entering non-english characters. For example, all Chinese, Japanese, Cyrllic, but also single characters like: à, â, ù, û, ü, ô, î, ê. Would this be possible? Do I have to set up a locale on my MVC application or rather just do a regex textbox validation? Just a side note, I want to be able to enter numbers and other characters. I only want this to exclude letters.

Please advice, thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For this you have to use Unicode character properties and blocks. Each Unicode code points has assigned some properties, e.g. this point is a Letter. Blocks are code point ranges.

For more details, see:

Those Unicode Properties and blocks are written p{Name}, where “Name” is the name of the property or block.

When it is an uppercase “P” like this P{Name}, then it is the negation of the property/block, i.e. it matches anything else.

There are e.g. some properties (only a short excerpt):

  • L ==> All letter characters.
  • Lu ==> Letter, Uppercase
  • Ll ==> Letter, Lowercase
  • N ==> All numbers. This includes the Nd, Nl, and No categories.
  • Pc ==> Punctuation, Connector
  • P ==> All punctuation characters. This includes the Pc, Pd, Ps, Pe, Pi, Pf, and Po categories.
  • Sm ==> Symbol, Math

There are e.g. some blocks (only a short excerpt):

  • 0000 – 007F ==> IsBasicLatin
  • 0400 – 04FF ==> IsCyrillic
  • 1000 – 109F ==> IsMyanmar

What I used in the solution:

P{L} is a character property that is matching any character that is not a letter (“L” for Letter)

p{IsBasicLatin} is a Unicode block that matches the code points 0000 – 007F

So your regex would be:

^[P{L}p{IsBasicLatin}]+$

In plain words:

This matches a string from the start to the end (^ and $), When there are (at least one) only non letters or characters from the ASCII table (doce points 0000 – 007F)

A short c# test method:

string[] myStrings = { "Foobar",
    "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fc9e0e0cfedeefd">[email protected]</a>!"§$%&/()",
    "Föobar",
    "fóÓè"
};

Regex reg = new Regex(@"^[P{L}p{IsBasicLatin}]+$");

foreach (string str in myStrings) {
    Match result = reg.Match(str);
    if (result.Success)
        Console.Out.WriteLine("matched ==> " + str);
    else
        Console.Out.WriteLine("failed ==> " + str);
}

Console.ReadLine();

Prints:

matched ==> Foobar
matched ==> [email protected]!”§$%&/()
failed ==> Föobar
failed ==> fóÓè

Method 2

You can use a Regular Expression attribute on your ViewModel to restrict that

public class MyViewModel
{
    [System.ComponentModel.DataAnnotations.RegularExpression("[a-zA-Z]+")]
    public string MyEntry
    {
       get;
       set;
    }
}

Method 3

You can use regex [x00-x80]+ or [u0000-u0080]+. Haven’t tested but think it should work in C# also.

Adapted from: Regular expression to match non-English characters?

You can use regex validation for textbox and validate on the server also.

Method 4

May be this one help You:=

private void Validate(TextBox textBox1)
{
 Regex rx = new Regex("[^A-Z|^a-z|^ |^t]");
 if (rx.IsMatch(textBoxControl.Text))
  throw new Exception("Your error message");
}

Usefull Link:-

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84e4f7fa-5fff-427f-8c0e-d478cb38fa12

http://www.c-sharpcorner.com/Forums/Thread/177046/allow-only-20-alphabets-and-numbers-in-textbox-using-reg.aspx

Method 5

this might help, not efficient way but simple non-reg validation

foreach (char c in inputTextField)
{
       if ((int)(c) > 127)
          {
             // expection or your logic whatever you want to return
          }

 }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x