Thursday 1 November 2012

Code for Text Field & UITextView Not Allowed Special Characters and Spaces in iPhone & iPad

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == aText)
   {
         if ([string isEqualToString:@"&"] || [string isEqualToString:@" "] ) 
        {
               return NO;
        }
   }
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == TextField)
{
        
        if ([string length] == 0 && range.length > 0)
        {
            TextField.text = [TextField.text stringByReplacingCharactersInRange:range withString:string];
            return NO;
        }
        
        NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
        if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
}






- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if(textView == YourReview1)
    {
        if ([text isEqualToString:@"&"]||[text isEqualToString:@" "])
        {
            return NO;
        }
        return YES;
    }
}

No comments: