I need a way to ensure that phone numbers have 10 digits with no other characters ie () - and make sure that email addresses are valid emails (formatted correctly).
Is there any library that can't make this easy for me so I don't have to write regular expressions.
Add a TextFormField with validation logicValidate the input by providing a validator() function to the TextFormField . If the user's input isn't valid, the validator function returns a String containing an error message. If there are no errors, the validator must return null.
So, we can minimize the code, and manage it well, by creating an extension of UITextField . Then, inside it, create a function that will be used to validate the TextField . That function should return a Bool value, as shown below: This is just a custom validation; you can create a validation based on your needs.
storyboard add one textField, one button and one label one below other as shown in the figure. On click of the button we will check whether the text field is empty or not and show the result in label. @IBOutlet weak var textField: UITextField! @IBOutlet weak var resultLabel: UILabel!
This will check a UITextField for a proper email and phone number of 10 digits or less.
Add this method to the textFields delegate then check if the characters it is about to change should be added or not.
Return YES
or NO
depending on the text field, how many characters are currently in it, and what characters it wants to add:
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMERIC @"1234567890"
#define ALPHA_NUMERIC ALPHA NUMERIC
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *unacceptedInput = nil;
switch (textField.tag) {
// Assuming EMAIL_TextField.tag == 1001
case 1001:
if ([[textField.text componentsSeparatedByString:@"@"] count] > 1)
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet];
else
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&'*+-/=?^_`{|}~@"]] invertedSet];
break;
// Assuming PHONE_textField.tag == 1002
case 1002:
if (textField.text.length + string.length > 10) {
return NO;
}
unacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
break;
default:
unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
break;
}
return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}
Also, check out these 2 articles:
Auto-formatting phone number UITextField on the iPhone
PhoneNumberFormatter.
Here's a simple way of ensuring phonenumber length in the UIViewController that has the text field in it's view.
- (void)valueChanged:(id)sender
{
if ([[[self phoneNumberField] text] length] > 10) {
[[self phoneNumberField] setText:[[[self phoneNumberField] text]
substringToIndex:10]];
}
}
- (void) viewWillAppear:(BOOL)animated
{
[[self phoneNumberField] addTarget:self
action:@selector(valueChanged:)
forControlEvents:UIControlEventEditingChanged];
}
For emails I suppose you want to check against a regexp when it loses focus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With