0
protected void grvCustomers_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
     var row = grvCustomers.Rows[e.RowIndex];
     var customerListIndex = row.DataItemIndex;
     var customerid = (int) grvCustomers.DataKeys[customerListIndex].Value;

     var newName = ((TextBox) row.Cells[1].Controls[0]).Text; //0 = id, 1 = Phonenumber, 2 = Name
     var newPhonenr = ((TextBox) row.Cells[2].Controls[0]).Text;
     if (newPhonenr.Length == 12)
     {
         _c.UpdateCustomer(customerid, newName, newPhonenr);
         grvCustomers.EditIndex = -1;
         grvCustomers.DataSource = _c.GetAllCustomers();
         grvCustomers.DataBind();
     }
     else
     {
         errormessage2.Text = "Fill in a correct phone number!";
         errormessage2.Visible = true;
     }
}

Hi guys, how can I check whether or not my newName contains a digit? I have used Regex.IsMatch() but I am trying to allow spaces as well in names such like John Doe or Christopher Philips. Basically, I'm trying to allow names with spaces but at the same time check if it's only a string. And I really don't one typing like X Æ A-12 Musk.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
dxve
  • 3
  • 2
  • I'm not sure exactly what you're trying to do but `Regex.IsMatch(newName, "\\d");` will return true if the `newName` contains one or more digits. – 41686d6564 stands w. Palestine May 14 '20 at 00:53
  • I am trying to find a solution to allow editing a name with a space in a gridview (like John Doe as I mentioned) without the name containing digits (John Doe123 for eg). – dxve May 14 '20 at 00:58
  • Well, as I said, you _could_ use `Regex.IsMatch()` to check if the input contains digits. Note, however, that in most situations, imposing restrictions on first and last names [isn't a good idea](https://stackoverflow.com/a/7363159/8967612) (unless you have a good reason). – 41686d6564 stands w. Palestine May 14 '20 at 01:01
  • @AhmedAbdelhameed not everyone likes [Elon Musk](https://www.bing.com/search?q=elon+musk+baby+name) enough to allow reasonably arbitrary names... :) – Alexei Levenkov May 14 '20 at 01:05
  • @AhmedAbdelhameed The reason why I would want that is because I am making an ASP.NET website which is actually the last and most important schoolproject this year. I am trying to not let my jury members find as many bugs as they can, so they can judge my work. – dxve May 14 '20 at 01:09
  • I've updated title to match accepted answer and picked duplicate. I've also provided the example OP wants to disallow. Feel free to [edit] post if you feel that accepted answer is not what you were asking for or you have improvements for the edit (like chaning Bing to Google link). – Alexei Levenkov May 14 '20 at 01:12
  • @AlexeiLevenkov Actually, I also wouldn't allow arbitrary names in all situations (users can and will abuse this if the product is big enough). That being said, too many restrictions will also frustrate users with valid names that don't fit "the criteria". – 41686d6564 stands w. Palestine May 14 '20 at 01:14

2 Answers2

0

what you want to do is: allow a-zA-z and space, but not allow 0-9, am i right?

Here is the regex ^[a-zA-Z ]*$ in C# you can try.

For those more specific characters, you can add them into the regex by yourself, e.g.:

^[a-zA-Z '.-]*$ to also allow ', ., and -.

Please note, the - should be the last one within [] because it is a predefined character by Regex, and it will be considered as arrange signal while betweened.

Shawn Xiao
  • 560
  • 5
  • 18
  • 1
    Congratulations! Now, you can't have someone named "d'Arras" (or someone having the "Jr." suffix or someone with a hyphen in their name or pretty much anyone with a name that contains a single non-English letter) use your app. – 41686d6564 stands w. Palestine May 14 '20 at 01:11
  • @AhmedAbdelhameed to be fair OP did not ask for [Regular expression for first and last name validation](https://stackoverflow.com/questions/2385701/regular-expression-for-first-and-last-name), but rather some unusable special case (I've edited question to be more ... clear?) – Alexei Levenkov May 14 '20 at 01:14
  • @AlexeiLevenkov True. The problem with this answer though is that "doesn't contain digits" != "contains only English letter and/or space characters". This might very well be what the OP wanted but it's definitely what was mentioned in the question. – 41686d6564 stands w. Palestine May 14 '20 at 01:19
  • @AhmedAbdelhameed this answer is an example of regular trolling of new users :) I would assume Shawn Xiao knows the proper answer to name validation (which I linked in previous comment), so they just go for something that looks correct in hope OP votes/accepted it. I guess I should add the second duplicate to be at least somewhat helpful. – Alexei Levenkov May 14 '20 at 01:29
  • Thanks Alexei Levenkov. And I also edit my answer to meet the specific characters @AhmedAbdelhameed mentioned. Please check and ask if any question. – Shawn Xiao May 14 '20 at 01:33
0

Okay, If I have understood it well, then I think you want to validate the first name and last name of a user and you do not want to allow the digits in it. There could be more than one solutions out of them, I am listing one here.

bool containsInt = "John Doe 1".Any(char.IsDigit)

This will return true if there is any digit in the string, otherwise false.

By the way, I would strongly want you to see this Answer on SO. I am copying some text from it:

You may as well allow all characters because "123 456" is really no worse a pseudonym than "Abc Def".

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42