import UIKit
class RegisteredViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var TopView: UIView!
@IBOutlet weak var registeredBtn: UIButton!
@IBOutlet weak var AgreeBtn: UIButton!
@IBOutlet weak var eMailField: UITextField!
@IBOutlet weak var PasswordField: UITextField!
@IBOutlet weak var PasswordCKField: UITextField!
@IBOutlet weak var NicknameField: UITextField!
@IBOutlet weak var BirthyearField: UITextField!
@IBOutlet weak var BirthmonthField: UITextField!
@IBOutlet weak var BirthdayField: UITextField!
var UserDef:UserDefaults!
var agreeCK:Bool!
var singleFingerTap:UITapGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
//監聽鍵盤動作
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWasShown), name: NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWasHidden), name: NSNotification.Name.UIKeyboardWillHide, object: nil);
singleFingerTap = UITapGestureRecognizer.init(target: self, action: #selector(handleSingleTap));
agreeCK = false;
UserDef = UserDefaults.standard
UserDef.set(agreeCK, forKey: "ck");
AgreeBtn.layer.borderWidth = 1.5;
AgreeBtn.layer.borderColor = UIColor(red: 77/255, green: 77/255, blue: 77/255, alpha: 1).cgColor
registeredBtn.layer.cornerRadius = 5;
self.eMailField.delegate = self;
self.PasswordField.delegate = self;
self.PasswordCKField.delegate = self;
self.NicknameField.delegate = self;
eMailField.clearButtonMode = .always;
PasswordField.clearButtonMode = .always;
PasswordCKField.clearButtonMode = .always;
NicknameField.clearButtonMode = .always;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
@IBAction func AgrddBtn(_ sender: UIButton) {
if (UserDef.bool(forKey: "ck") == false){
agreeCK = true;
UserDef.set(agreeCK, forKey: "ck");
print(UserDef.bool(forKey: "ck"))
AgreeBtn.setBackgroundImage(UIImage(named:"tick"), for: .normal)
}else{
agreeCK = false;
UserDef.set(agreeCK, forKey: "ck");
print(UserDef.bool(forKey: "ck"))
AgreeBtn.setBackgroundImage(UIImage(named:""), for: .normal)
AgreeBtn.backgroundColor = UIColor.white;
}
}
@IBAction func RegisteredBtn(_ sender: UIButton) {
if(UserDef.bool(forKey: "ck")==true){
if(eMailField.text != "" && PasswordField.text != "" && NicknameField.text != "" && BirthyearField.text != "" && BirthmonthField.text != "" && BirthdayField.text != "" && PasswordCKField.text != ""){
if (self.isValidEmail(testStr: eMailField.text!) == false){
print("信箱格式錯誤");
let alertcontroller = UIAlertController(title: "Error", message: "The mailbox is malformed", preferredStyle: .alert);
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true, completion: nil)
}else{
if(PasswordCKField.text == PasswordField.text){
// print(PasswordField.text?.characters.count)
if((PasswordField.text?.characters.count)! < 6 || (PasswordField.text?.characters.count)! > 12){
let alertcontroller = UIAlertController(title: "Error", message: "The character should be between 6 and 12", preferredStyle: .alert);
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true, completion: nil)
print("密碼未介於6~12之間")
}else{
//註冊成功
print("註冊")
}
}else{
let alertcontroller = UIAlertController(title: "Error", message: "The passwords do not match", preferredStyle: .alert);
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true, completion: nil)
}
}
}else{
if(eMailField.text == ""){
eMailField.attributedPlaceholder = NSAttributedString(string: "Please enter email", attributes: [NSForegroundColorAttributeName: UIColor.red]);
}
if(PasswordField.text == ""){
PasswordField.attributedPlaceholder = NSAttributedString(string: "Please enter password", attributes: [NSForegroundColorAttributeName: UIColor.red]);
}
if(PasswordCKField.text == ""){
PasswordCKField.attributedPlaceholder = NSAttributedString(string: "Please enter password confirm", attributes: [NSForegroundColorAttributeName: UIColor.red]);
}
if(NicknameField.text == ""){
NicknameField.attributedPlaceholder = NSAttributedString(string: "Please enter nickname", attributes: [NSForegroundColorAttributeName: UIColor.red]);
}
if(BirthyearField.text == "" || BirthmonthField.text == "" || BirthdayField.text == ""){
let alertcontroller = UIAlertController(title: "Error", message: "Please enter your borthday", preferredStyle: .alert);
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true, completion: nil)
}
}
}else{
let alertcontroller = UIAlertController(title: "Error", message: "Please agree membership terms", preferredStyle: .alert);
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil);
alertcontroller.addAction(alertAction)
self.present(alertcontroller, animated: true, completion: nil)
}
}
// MARK: e-Mail檢查
func isValidEmail(testStr:String) -> Bool {
// print("validate calendar: \(testStr)")
let stricterFilter:Bool = false;
let stricterFilterString:String = "[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"
let laxString:String = ".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*"
let emailRegex:String = stricterFilter ? stricterFilterString : laxString;
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegex)
return emailTest.evaluate(with: testStr)
}
// MARK: TEXTField按下鍵盤return後動作
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
if(textField.isEqual(eMailField)){
PasswordField.becomeFirstResponder()
}else if(textField.isEqual(PasswordField)){
PasswordCKField.becomeFirstResponder()
}else if(textField.isEqual(PasswordCKField)){
NicknameField.becomeFirstResponder()
}else if(textField.isEqual(NicknameField)){
NicknameField.resignFirstResponder()
}
return true;
}
//MARK: 鍵盤監聽動作(當用戶點view收起)
func keyboardWasShown(){
TopView.addGestureRecognizer(singleFingerTap);
}
func keyboardWasHidden(){
TopView.removeGestureRecognizer(singleFingerTap);
}
func handleSingleTap(){
//Do stuff here...
eMailField.resignFirstResponder()
PasswordField.resignFirstResponder()
PasswordCKField.resignFirstResponder()
NicknameField.resignFirstResponder()
}