Validation issues for user names and passwords in C #.

Source: Internet
Author: User

The purpose of this exercise is to use LINQ to XML, regular expressions, to practice using serialization and deserialization on this basis tomorrow, and continue to add a little bit of functionality.

First, this is a form program, designed as follows:

The XML that holds the user name and password is as follows:

The implementation code is as follows:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.Text.RegularExpressions; One usingSystem.Xml; A usingSystem.Xml.Linq; -  - namespaceCheckinfo the { -      Public Partial classForm1:form -     { -          PublicForm1 () +         { - InitializeComponent (); +         } A  at         Private voidtextBox1_TextChanged (Objectsender, EventArgs e) -         { -             if(TextBox1.Text = ="Please enter user name, format: Qarootdc\\jqhuang") -             { -TextBox1.Text =""; -             } in  -         } to  +         Private voidTextbox2_textchanged_1 (Objectsender, EventArgs e) -         { the             if(TextBox2.Text = ="Please enter your password") *             { $TextBox2.Text ="";Panax Notoginseng             } -         } the  +         Private voidButton1_Click (Objectsender, EventArgs e) A         { the             if(IsValidUserName (textbox1.text) = =false) +             { -MessageBox.Show ("user name is not properly formatted! Please re-enter! "); $TextBox1.Text =""; $             } -             Else  -             { the                 //The user name is formatted correctly. - checkuserandpwd (textbox1.text,textbox2.text);Wuyi             } the         } -  Wu         Private voidCheckuserandpwd (stringUsernamestringpwd) -         { About             //read Userinfo.xml detect if user exists $XDocument UserInfo = Xdocument.load (@"C:\Users\jqhuang\Desktop\UserInfo.xml"); -             varresult = fromUserelementinchUserinfo.element ("System"). Element ("Users"). Elements ()whereUserelement.element ("username"). value.tostring () = = TextBox1.Text.ToString ()SelectUserelement.element ("pwd"). Value; -             if(Result! =NULL) -             { A                 foreach(varPasswordinchresult) +                 { the                     if(Password = =pwd) -                     { $MessageBox.Show ("user name and password match successfully! "); the                     } the                     Else the                     { theMessageBox.Show ("user name and password do not match, please re-enter the password"); -TextBox2.Text =""; in                     } the                 } the             } About             Else  the             { theMessageBox.Show ("the user you entered does not exist! "); the             } +         } -  the         BOOLIsValidUserName (stringuserName)Bayi         { the             returnRegex.IsMatch (UserName,@"^.+\\.+$"); the         } -     } -}

Run as follows--

1, the user name does not exist in the case:

/

2, the user name and password does not match the situation:

3, the user name format is not correct (with regular expression validation):

4, the user name and password matching successful situation:

Validation issues for user names and passwords in C #.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.