Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I do it:</p> <pre><code>// Define some constants: #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" #define NUMERIC @"1234567890" #define ALPHA_NUMERIC ALPHA NUMERIC // Make sure you are the text fields 'delegate', then this will get called before text gets changed. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // This will be the character set of characters I do not want in my text field. Then if the replacement string contains any of the characters, return NO so that the text does not change. NSCharacterSet *unacceptedInput = nil; // I have 4 types of textFields in my view, each one needs to deny a specific set of characters: if (textField == emailField) { // Validating an email address doesnt work 100% yet, but I am working on it.... The rest work great! if ([[textField.text componentsSeparatedByString:@"@"] count] &gt; 1) { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet]; } else { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&amp;'*+-/=?^_`{|}~@"]] invertedSet]; } } else if (textField == phoneField) { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMERIC] invertedSet]; } else if (textField == fNameField || textField == lNameField) { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet]; } else { unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet]; } // If there are any characters that I do not want in the text field, return NO. return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] &lt;= 1); } </code></pre> <p>Check out the <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html" rel="noreferrer">UITextFieldDelegate Reference</a> too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload