Im doing a conditional statement that checks if a textfield contains certain characters, if it does they an UIAlertView will show. At the moment if the text field contains letters then an the alert is shown, how would I extend my method below to include characters such as ? ! , $ etc.
if ([self.withdrawTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location != NSNotFound) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
Try this:
NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mcs1 addCharactersInString:#"?!.$"];
if ([self.withdrawTextField.text rangeOfCharacterFromSet:mcs1].location != NSNotFound) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
As you want only numbers and dot in the string than you can try below code. It may help you.
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789."];
for (int i = 0; i < [[self.withdrawTextField.text length]; i++)
{
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c])
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
Related
I have created a US-format mobile number. This code does not display letters and special characters. It successfully displays but it displays UIAlertviews, when the user enters letters and special characters in UITextfield, it shows some UIAlertview. How can I control the UIAlertviews? I want to display only one Alertview.
This is the code that I already have:
if(textField==mobileNo)
{
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
for (int i = 0; i < [string length]; i--) {
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Alphbets and Special characters not allowed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return NO;
}
}
}
You could try the following... but its not a great solution.....
In your header declare a BOOL to check if the alert view has been shown already, like so:
BOOL alertCheck;
In your viewDidLoad set it to NO:
alertCheck = NO;
Then in your code, check it before deciding to display your alertview.
if(textField==mobileNo) {
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
for (int i = 0; i < [string length]; i--) {
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
if (alertCheck == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Alphbets and Special characters not allowed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
alertCheck = YES;
}
return NO;
}
}
}
You can try the following:
if (![myCharSet characterIsMember:c]) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//show the UIAlertView
});
}
That code isn't so nice. You're just checking if the string value is a number or not. To do that you can use [NSScanner scannerWithString:] this way:
bool isNumber = [[NSScanner scannerWithString: string] scanInt:nil]
if (!isNumber) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Alphbets and Special characters not allowed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
Code seems to be clearer and a lot more simpler.
Hope it helped!
am getting following response from web service and i need to fetch the string
Error: no record found in database.
shown below:
d = (
"Error: no record found in database."
);
here is my code
if([[[[dictiona objectForKey:#"d"] objectForKey:#""] objectAtIndex:0] isKindOfClass:[NSString class]] == YES)
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"No Record Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[myAlertView show];
}
Your code should not check for a second objectForKey, as there isn't a second NSDictionary to check for, because your second item is an NSArray, which doesn't respond by keys. This should work for you instead:
if ([[[dictionary objectForKey:#"d"] objectAtIndex:0] isKindOfClass:[NSString class]])
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"No Record Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[myAlertView show];
}
In my Calorie Tracker App, I have two textfields. One is for the name of the food, and the other is for the amount of calories.To check and see if they are filled I wrote the following code:
if([foodString isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not entered the name of the meal!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not enteted the amount of calories!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual:#" "] && [foodString isEqual:#" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You haven't entered anything!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
So for the one where the amountText textfield isn't filled out, but the nameOfFood is filled out, there is an alert. For the rest however, there is no alert. If anyone could provide the proper syntax for checking to see if my textfields are being filled out properly, it would be much appreciated.
Remember, my first textfield is a string value, and the other is an integer value.
Thanks in advance.
I've written a simple app which validates user input (whether NULL or longer than a define length). It should return validation error messages when validation fails and otherwise, redirect to another page.
However, the app only returns the messge for the first condition (Username is Empty) for all scenarions. (Such as username is filled and password is empty, etc.)
m file:
- (IBAction)doLogin {
if(uname.text==NULL) {
UIAlertView *err1 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Username is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err1 show];
NSLog(#"%#",uname.text);
}
else if(passw.text==NULL) {
UIAlertView *err2 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Password is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err2 show];
NSLog(#"%#",passw.text);
}
else if (uname.text.length < 6)
{
UIAlertView *err3 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a username longer than 6 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err3 show];
NSLog(#"%#",uname.text);
}
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
else {
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Thank you" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"OK", nil];
[alert show];*/
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:#"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];
}
An alternative to karthika (but using similar structure) this will provide feedback on the entire form in a single message. Perhaps a little more user friendly and certainly reduces negative user interaction.
-(BOOL)isFormDataValid{
NSMutableArray *errorMessages = [[NSMutableArray alloc] init];
if([self.emailTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter email",nil)];
}
if([self.passwordTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter password",nil)];
}
if ([errorMessages count]) {
NSString * msgs = [errorMessages componentsJoinedByString:#"\n"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Whoops!",nil) message:msgs delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return NO;
} else {
return YES;
}
}
-(BOOL)isFormDataValid{
NSString *errorMessage = nil;
UITextField *errorField;
if([nameTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter username";
errorField = nameTextField;
}
else if([[nameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = nameTextField;
}
else if([passwordTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter password";
errorField = passwordTextField;
}
else if([[passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = passwordTextField;
}
if (errorMessage) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed!" message:errorMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[errorField becomeFirstResponder];
return NO;
}else{
return YES;
}
}
Apart from the fact that you do
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
instead of
else if (passw.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",passw.text);
}
Your code should work just fine.
Also, bear in mind that a text field's text won't be nil, it will just be an empty string (lenght == 0), unless you explicitly set it to nil.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i am getting response like {"nok"} .... now i want to compare this for check status of data sent or not.
this is my code
NSString *responseStr = [sender responseString];
NSLog(#"response string %#", responseStr);
if([responseStr isEqualToString:#"nok"])
{
NSLog(#"Hiii");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Attenzione: si è verificato un errore di richiesta del premio." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Inviato con successo" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
[self startIscrivity2View];
}
it's always run else part...
please help me
USe the below function of NSString.
- (NSRange)rangeOfString:(NSString *)aString
The above function will not check for the equality, it will tell u whether your responseStr contain "nok" string OR not ..
NSRange myStringRange = [responseStr rangeOfString:#"nok"] ;
if(myStringRange.location != NSNotFound )
{
NSLog(#"Hiii");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Attenzione: si è v erificato un errore di richiesta del premio." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Inviato con successo" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
[self startIscrivity2View];
}
}
Try:
if([responseStr isEqualToString: #"nok"])
{
...
}
Try replacing
responseStr == #"nok"
with
[responseStr isEqualToString:#"nok"];
In your code, the variable is not checking if the characters in the string are the same, it is checking if they are pointing to the same location in memory.
Try isEqualToString
if([responseStr isEqualToString:#"nok"]){
}
or compare
if([responseStr compare:#"nok"] == NSOrderedSame){
}
Never compare 2 strings using == operator.
Try.
if([responseStr isEqualToString: #"{\"nok\"}"] == TRUE)
{
....
}
else
{
.....
}
Or you can remove all exara characters from response string and then compair it.
Like -
NSString *str = [responseStr stringByReplacingOccurrencesOfString:#"{" withString:responseStr];
str = [responseStr stringByReplacingOccurrencesOfString:#"}" withString:responseStr];
str = [responseStr stringByReplacingOccurrencesOfString:#"\"" withString:responseStr];
if([responseStr isEqualToString: #"nok"] == TRUE)
{
....
}
else
{
.....
}
Or you can try.
NSRange myStringRange = [responseStr rangeOfString:#"nok"] ;
if(myStringRange.location != NSNotFound )
{
...
}
else
{
...
}