Last active
November 19, 2020 11:13
-
-
Save ahmet-cetinkaya/4208a797be823be60051f630fe45cb79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Windows.Forms; | |
using System.Xml.Linq; | |
namespace saveAsXml | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
bool[] inputsExist = new bool[4] { String.IsNullOrEmpty(tbName.Text), String.IsNullOrEmpty(tbEmail.Text), String.IsNullOrEmpty(tbNickName.Text), String.IsNullOrEmpty(tbPassword.Text) }; | |
if (inputsExist.Any(x => x)) | |
{ | |
MessageBox.Show("Gerekli alanları doldurunuz."); | |
return; | |
} | |
Regex emailRegex = new Regex("^(?(\")(\".+?(?<!\\\\)\"@)|(([0-9a-z]((\\.(?!\\.))|[-!#\\$%&'\\*\\+/=\\?\\^`\\{\\}\\|~\\w])*)(?<=[0-9a-z])@))(?(\\[)(\\[(\\d{1,3}\\.){3}\\d{1,3}\\])|(([0-9a-z][-\\w]*[0-9a-z]*\\.)+[a-z0-9][\\-a-z0-9]{0,22}[a-z0-9]))$"), | |
strongPasswordRegex = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})"); | |
if (!emailRegex.Match(tbEmail.Text.ToLower()).Success) | |
{ | |
MessageBox.Show("E Posta adresi geçersiz."); | |
return; | |
} | |
if (!strongPasswordRegex.Match(tbPassword.Text).Success) | |
{ | |
MessageBox.Show("Şifreniz en az 8 karakter uzunluğunda ve en az 1 küçük harf, 1 büyük harf, 1 rakam barındırmalıdır."); | |
return; | |
} | |
string usersXmlFileName = "users.xml"; | |
if (!File.Exists(usersXmlFileName)) | |
{ | |
XDocument newDoc = new XDocument( | |
new XElement("Users") | |
); | |
newDoc.Save(usersXmlFileName); | |
} | |
XDocument doc = XDocument.Load(usersXmlFileName); | |
bool emailExist = doc.Descendants("Email").Any(x => x.Value == tbEmail.Text.ToLower()), | |
nickNameExist = doc.Descendants("NickName").Any(x => x.Value.ToLower() == tbNickName.Text.ToLower()); | |
if (emailExist) | |
{ | |
MessageBox.Show("E Posta adresi kullanılmaktadır."); | |
return; | |
} | |
if (nickNameExist) | |
{ | |
MessageBox.Show("Kullanıcı adı kullanılmaktadır."); | |
return; | |
} | |
XElement user = new XElement("User", | |
new XElement("Name", tbName.Text), | |
new XElement("Email", tbEmail.Text.ToLower()), | |
new XElement("NickName", tbNickName.Text), | |
new XElement("Password", tbPassword.Text) | |
); | |
doc.Root.Add(user); | |
doc.Save(usersXmlFileName); | |
MessageBox.Show("Kayıt işlemi başarılı."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment