I have a UITextField and in that I should be able to type only integers (no text) and after that the integer values have to be passed to an NSMutableDictionary. Then from this dictionary I have to post to webserver.
Post UITextfield integer values to web server
890 views Asked by user3932799 At
4
There are 4 answers
0
On
This one is the best way to do such things, make this following lines in NSString Category and check a validation as per your need.
-(NSString *)isTextFieldHasNumberOnly
{
NSString *strMessage;
NSString *enteredText=[self trimWhiteSpace];
if ([enteredText length]==0)
{
strMessage=@"Please enter number.";
return strMessage;
}
NSString *numbEx = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
NSRegularExpression *numbRegEx = [NSRegularExpression regularExpressionWithPattern:numbEx options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [numbRegEx numberOfMatchesInString:enteredText options:0 range:NSMakeRange(0, [enteredText length])];
if (numberOfMatches == 0)
strMessage=@"Please enter valid number.";
else
strMessage=@"";
return strMessage;
}
0
On
//Create a textfield
txt = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 150, 50)];
txt.placeholder = @"enter here";
txt.borderStyle = UITextBorderStyleRoundedRect;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[txt setClearButtonMode:UITextFieldViewModeWhileEditing];
[txt setKeyboardType:UIKeyboardTypeNumberPad];
txt.delegate=self;
[cell.contentView addSubview:txt];
You could check whether entered text is number or not:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [Height.text stringByReplacingCharactersInRange:range withString:string];
if([newString length] > 3)
{
UIAlertView *obj_AlertView=[[UIAlertView alloc]initWithTitle:@""
message:@"Enter 3 digit only"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[obj_AlertView show];
}
return !([newString length] > 3);
}
now create a NSMutabledictionary in your button action to post
-(IBAction)btnClicked:(id)sender
{
UIButton *button = (UIButton *)sender;
button.tintColor=[UIColor redColor];
NSLog(@"%@",button);
NSMutableDictionary *datadict=[NSMutableDictionary dictionary];
now pass your textfield value to nsmutabledictionary to post
if (txt.text !=NULL) {
[datadict setValue:[NSNumber numberWithInt:[txt.text intValue]] forKey:@"txt"];
// [datadict setObject:[NSString stringWithFormat:@"%@",txt.text] forKey:@"txt"]; (if it is for NSdictionary)
} else {
[datadict setObject:[NSString stringWithFormat:@" "] forKey:@"txt"];
}
now post the value to web-server
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" your post web-server link here"];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);
Change the keyboard type of
UITextFieldto Number Pad so it only takes numbers:You can also check whether the text entered is a number or not:
You can save the value in dictionary by using following code:
OR