Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I handled the problem on a SSN verification field, you can modify the max length and remove the <code>if</code> statement checking for keyboard type if you need to.</p> <p>There is also logic to suppress the max length alerts when the user is typing as opposed to pasting data.</p> <p>In the context of this code, <code>BasicAlert()</code> is a <code>#define</code> macro that simply shows a <code>UIAlertView</code> or <code>UIAlertController</code> using the title and message strings passed.</p> <pre><code>// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the object that will contain this code, because otherwise it would never be called. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // allow backspace if (!string.length) { return YES; } // Prevent invalid character input, if keyboard is numberpad if (textField.keyboardType == UIKeyboardTypeNumberPad) { if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound) { // BasicAlert(@"", @"This field accepts only numeric entries."); return NO; } } // verify max length has not been exceeded NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (proposedText.length &gt; 4) // 4 was chosen for SSN verification { // suppress the max length message only when the user is typing // easy: pasted data has a length greater than 1; who copy/pastes one character? if (string.length &gt; 1) { // BasicAlert(@"", @"This field accepts a maximum of 4 characters."); } return NO; } // only enable the OK/submit button if they have entered all numbers for the last four of their SSN (prevents early submissions/trips to authentication server) self.answerButton.enabled = (proposedText.length == 4); return YES; } </code></pre>
    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. 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