Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Be aware the previous answer is woefully inadequate. Heaven forbid your user enter an incorrect digit and dare attempt to delete it! In fairness, the poster noted the code may not work perfectly. But then, it wouldn't even compile, so the buyer beware filter should already be high. If you fix the compile error and try the code, you'll see you can easily end up with input that does not match the poster's stated format.</p> <p>Here's a solution I've used for restricting a text field to a phone number of the format 123-456-7890. Adjusting for other numeric formats is trivial. Note the use of the passed <strong>NSRange</strong>. And BTW, rejecting non-digit characters is needed even when using a numeric virtual keyboard since users can still enter non-digits via a hardware keyboard.</p> <p>One other note. I add the hyphen <strong>after</strong> the entry of the 4th and 7th digits to make the deleting of digits a bit easier. If you add after the 3rd and 6th digits, you will have to handle the case of deleting the dangling hyphen. The code below avoids that use case.</p> <pre><code>// Restrict entry to format 123-456-7890 - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // All digits entered if (range.location == 12) { return NO; } // Reject appending non-digit characters if (range.length == 0 &amp;&amp; ![[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[string characterAtIndex:0]]) { return NO; } // Auto-add hyphen before appending 4rd or 7th digit if (range.length == 0 &amp;&amp; (range.location == 3 || range.location == 7)) { textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string]; return NO; } // Delete hyphen when deleting its trailing digit if (range.length == 1 &amp;&amp; (range.location == 4 || range.location == 8)) { range.location--; range.length = 2; textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""]; return NO; } return YES; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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