If a user registers for example and types "Nathan" and when he tries to login he writes "nathan" instead, how can I make so that the "big letter" in the word gets ignored?
-
3This may help you : http://stackoverflow.com/questions/6371150/comparing-two-strings-ignoring-case-in-c-sharp – Arnaud Dec 22 '15 at 12:54
-
Before comparing the user input you could make it lower case: `input.ToLower()`? – garglblarg Dec 22 '15 at 12:56
-
just convert user input and your stored value into similar format like convert both toLower ot ToUpper. – Vaibhav Chaurasiya Dec 22 '15 at 13:07
-
How do I make it so it works on either if u type with a big letter or without? Now it only works 1 way. – DiddanDo Dec 22 '15 at 15:05
-
Did you try my solution? Is this question closed? – Majkl Jan 20 '16 at 10:58
3 Answers
usually such a problem is solved as follows:
if (a.ToLower() == b.ToLower())
{
}
or when registering him save ït as lowercase
UPDATE
If for both string you use ToLower (), so it does not matter if some of the letters has Upper Letter. Every letter will be lowerCase and than string will be same (try your self). A better solution is to store login in database as a user has entered and ToLower () use when checking login, see a clear example:
string registerName = "Nathan"; // in database will be "Nathan"
string loginName = "nathan"; // user try login with "nathan"
if (registerName.ToLower() == loginName.ToLower())
{// login correct
}
- 765
- 1
- 9
- 25
-
Ok so it works when i login with a small letter but not with a big letter string CapsUser = App.ourUsername.ToLower (); string CapsUserHigh = App.ourUsername.ToUpper (); – DiddanDo Dec 22 '15 at 14:58
-
@DiddanDo Why are you using `ToUpper()`? You should just use both `ToLower()` like Majkl said – komodosp Dec 22 '15 at 16:28
-
Hm okay. I register with a label named : yourEmail (x:name in xaml) and when I enter my "email" I have a string called : App.ourUsername, should I do it like this in my "login" button: string CapsUserHigh = yourEmail.Text.ToUpper (); ? i can't get it to work hmm.. – DiddanDo Dec 22 '15 at 17:58
You can either apply java script so that name always typed in required formatting or validate at server side. The best approach is to convert the name into upper or lower case and match it with the entry stored in a database. The result from database can also be converted to upper or lower case before matching.
For example
string name = "Nathan";
string upperString = name.ToUpper();
OR
string lowerString = name.ToLower();
- 156
- 3
-
Okay. It works when I login without caps but not the other way around. string CapsUser = App.ourUsername.ToLower (); string CapsUserHigh = App.ourUsername.ToUpper (); – DiddanDo Dec 22 '15 at 14:58
-
Use either lower or upper. If you are matching 2 strings, convert both to lower/upper and match. How you are comparing the username with a stored value ? – DevGuru Dec 22 '15 at 15:04
-
Hm okay. I register with a label named : yourEmail (x:name in xaml) and when I enter my "email" I have a string called : App.ourUsername, should I do it like this in my "login" button: string CapsUserHigh = yourEmail.Text.ToUpper (); ? i can't get it to work hmm.. – DiddanDo Dec 22 '15 at 15:11
-
This should work or to make it more simple use if (String.Equals(App.ourUsername, yourEmail.Text, StringComparison.InvariantCultureIgnoreCase)) { // do login } Else { // do not login and display some message } This method compares 2 strings and ignore the upper and lower case. – DevGuru Dec 22 '15 at 16:29
-
-
You need to add namespace "System" in your class. `Using System;` [String Comparer Class MSDN Example](https://msdn.microsoft.com/en-us/library/system.stringcomparer(v=vs.110).aspx) – DevGuru Dec 23 '15 at 09:56
-
Well, why not use appropriate comparer/comparison?
In case you want to test two strings only
String a = "Nathan";
String b = "naTHaN";
if (String.Equals(a, b, StringComparison.InvariantCultureIgnoreCase)) {
...
}
In case you want to have a collection, say Dictionary, provide a comparer:
Dictionary<String, User> users =
new Dictionary<String, User>(StringComparer.InvariantCultureIgnoreCase) {
{"Me", new User(...)},
{"Nathan", new User(...)},
};
So you can use the Dictionary as usual:
if (users.ContainsKey("nATHan")) {
..
}
if (users.TryGetValue("nathaN", out loggedUser)) {
...
}
- 180,369
- 20
- 160
- 215