如何检查在UITextField中输入的字符串是否是Swift3中的数字或字符串?

时间:2017-06-30 15:02:38

标签: ios swift3 uitextfield xcode8

我有一个UITextField,我们可以在其中输入电子邮件ID或我们的电话号码。我的问题是可以检查在UITextField中输入的文本是否是Swift3中按钮单击时的电话号码或emailID? 目前我使用以下代码:

I think your error is here 

$sql1 = "UPDATE user_earning SET points = points - '$points' WHERE user_number = '$number'";

You need the following steps
1. First retrieve the points from your table and store in a variable

$q=mysqli_query($con,"select * from user_earning");
$current_points = 0;
if($q){
    while($row = mysqli_fetch_assoc($q))
        $current_points = $row['points'];
}

2. Subtract the points as you want`enter code here`
$new_points = $current_points - $points.

3. Then do your update
$sql1 = "UPDATE user_earning SET points = '$new_points' WHERE user_number = '$number'";

但它可以验证,输入是否为整数。是否可以知道输入是emailID还是电话号码?

1 个答案:

答案 0 :(得分:1)

// Datatype specifier
enum DataType: Int {
  case Other = 0  // This can be string
  case Number = 1
  case Email = 2
}



// Validate Email
func isEmail(emailString: String) -> Bool {
    // Sample regex for email - You can use your own regex for email
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegex)
    return emailTest.evaluate(with: emailString)
}


// Check Datatype
func checkDataType(text: String)-> DataType {
    if let intVal = text.toInt() {
        return DataType.Number
    } else if isEmail(emailString: text) {
       return DataType.Email
    } else {
       return DataType.Other
   }
}

// print datatype
let dataType = checkDataType(text: "Your Input String")
print("DataType = \(dataType)")