Showing an input that only allows numbers and the decimal point? - iphone

Is there any way to define an <input> that shows a keyboard input that only allows numbers and the decimal point (or comma for international users)?
<input type='tel'> shows phone crap I don't need, and no decimal point
<input type='number' pattern='[0-9]*'> shows all numbers, but no decimal point
What can I do to get the input I need?

You can try this attribute to your type="number" field:
pattern="\d+(\.\d*)?"
this will allow only digits with/without a decimal point and the floating numbers on the right.

Please see my project of the cross-browser filter of value of the text input element on your web page using JavaScript language: Input Key Filter . You can filter the value as an integer number, a float number, or write a custom filter, such as a phone number filter. See an example of code of input a float number
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Input Key Filter Test</title>
<meta name="author" content="Andrej Hristoliubov anhr#mail.ru">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- For compatibility of IE browser with audio element in the beep() function.
https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible -->
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css">
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script>
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script>
</head>
<body>
<h1>Float field</h1>
Float field:
<input id="Float"
onchange="javascript: onChangeFloat(this)"
onblur="inputKeyFilter.isNaN(parseFloat(this.value), this);"
/>
<script>
CreateFloatFilter("Float");
function onChangeFloat(input){
inputKeyFilter.RemoveMyTooltip();
var elementNewFloat = document.getElementById("NewFloat");
var float = inputKeyFilter.parseFloat(input.value);
if(inputKeyFilter.isNaN(float, input)){
elementNewFloat.innerHTML = "";
return;
}
elementNewFloat.innerHTML = float + " or localized value: " + float.toLocaleString();
}
</script>
New float: <span id="NewFloat"></span>
</body>
</html>
Also see my page "Float field:" of the example of the input key filter

If you were doing this in a bona fide iOS app, you could probably add buttons of your own on top of the keyboard, based on this question, which is about the accessory view but the same process would work.
Otherwise, IamChuckB is right and the five keyboard types given in the Apple Developer's Library are exhaustive.

=> It will allow only single decimal dot (.)
=> It will only allow three digit decimal values after dot. you can modified by changing to {1,3} into below RegEx.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (!string.length)
return YES;
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = #"^([0-9]+)?(\\.([0-9]{1,3})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches == 0)
return NO;
return YES;
}

Related

how do i change contents of html file in objective c when file load in iphone

hi i am loading google map streetview in my webview and i want i enter the location in uitextfield it should change the lat long of html file how can do that this is the code of html file
<html>
<head>
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Embedded StreetView</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var bryantPark = new google.maps.LatLng(37.869260, -122.254811);
var panoramaOptions = {
position:bryantPark,
pov: {
heading: 165,
pitch:0,
zoom:1
}
};
var myPano = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
myPano.setVisible(true);
}
</script>
</head>
<body onload="initialize()">
<div id="pano" style="width: 425px; height: 240px"></div>
</body>
</html>
i am loading this html file in webview and after that i wanted to change the lat long after passing address in my uitextfield but i don't know how can i change the html file on run time
You can change it using javascript. For example, in order to add a new paragraph to the body by writing the following code:
NSString *js = #"document.body.appendChild(document.createElement(\"p\"));";
[webView stringByEvaluatingJavaScriptFromString:js];

Is this XPath query on parsing XHTML wrong? using TouchXML

I have been trying to parse a XHTML doc via TouchXML, but it always can't find any tags via XPath query.
Below is the XHTML:
XHTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" />
<title></title>
</head>
<body>
<p>
<a href="http://www.flickr.com/photos/55397648#N00/5987335786/"
title="casavermeer5.jpg by the style files, on Flickr">
<img src="http://farm7.static.flickr.com/6127/5987335786_abec990554_o.jpg"
width="500" height="750" border="0" alt="casavermeer5.jpg" />
</a>
</p>
</body>
</html>
So, we can see there are a "p" tag, "a" tag and "img" tag
What I did then is shown as the code below:
CXHTMLDocument *doc = [[[CXHTMLDocument alloc] initWithXHTMLString:XHTML options:0 error:&error] autorelease];
NSLog(#"error %#", [error localizedDescription]);
NSLog(#"doc children count = %d", [doc childCount]);
NSArray *imgNodeArray = [doc nodesForXPath:#"//img" error:&error];
NSLog(#"imgNodeArray = %d", [imgNodeArray count]);
NSLog(#"error %#", [error localizedDescription]);
The results are
error (null)
doc children count = 2
imgNodeArray = 0
error (null)
So, there are no error at all in parsing the XHTML doc and no error for the XPath query. Also this doc has two children under the root ("body" tag and "head" tag). But the problem is it cannot find the "img" tag. I have tried to replace "img" with other possible tag names (such as p, a, even body, head), no luck at all.
Can someone help me here?
P.S.
Actually the original doc is a HTML, I have used CTidy class in TouchXML lib to tidy the HTML to XHTML first. The XHTML above came from that CTidy results.
I also tried to add a namespace thing to the XPath query, like this
NSMutableDictionary *namespaceDict = [NSMutableDictionary dictionary];
[namespaceDict setValue:#"http://www.w3.org/1999/xhtml" forKey:#"xhtml"];
And change the XPath query to
NSArray *imgNodeArray = [doc nodesForXPath:#"//xhtml:img" namespaceMappings:namespaceDict error:&error];
Still no luck, can't find any results.
Try this //img.
When you use // it gets the img tag, no matter where it is in the page.
It is better than //xhtml:img - because sometimes the hierarchic tags change a bit in the code behind, so it is better to be global, and not too much specific.
I had a similar problem once that might help you. I had a document that I would parse and find certain landmarks and record their XPaths. Then, I would load the document into a UIWebView and run JavaScript to perform actions on the elements that I had previously marked. Problematically, the DOM structure was completely different after parsing the document and all my XPaths were invalid. One particular case related to tables.
<table>
<tr>
<td>Cell</td>
</tr>
</table>
The simple HTML above would always be converted to something like below. (The white space is only for readability and I'm going from memory.)
<table>
<thead></thead>
<tbody>
<tr>
<td>Cell</td>
</tr>
</tbody>
</table>
My point with this is that your parser may have injected elements into your HTML structure.

Get the right node from duplicates xml with NSXML

I'm trying to us NSXML to parse a user's channel from youtube. Parsing works ok, but I can't seem to figure out how to get the link from any specific movie as their are 5 exact the same nodes as following:
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=vLleTDikufefbk&feature=youtube_gdata" />
<link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLleTDikededubk/responses" />
<link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLlededTDikubk/related" />
<link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=vLldedeeTDikubk" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/node/uploads/vLlgrgreTDikubk" />
I've figured out how to get the attribute href from the node link. But since their are 5 different links I don't know how to only select the first one. Anyone got an idea?
Thnx you guys!!!
Found the solution already. I'll check if the link node has an attribute with alternate in it. If it does it has the right link node. Here is the code:
NSMutableArray *link; if ([elementName isEqualToString:#"link"]) if (!link) link = [[NSMutableArray alloc] init]; NSString *alternate = [attributeDict objectForKey:#"rel"]; if([alternate isEqualToString:#"alternate"]){ NSString *href = [attributeDict objectForKey:#"href"]; alternate = NULL; }

Reading data from server script using URL

I am calling a login script on the server using http call from iphone.
the script returns a string "Invalid" or "valid" based on given username/pswd.
Here is what I am using:
NSString *myurlstr = [[NSString alloc] initWithFormat:#"http://www.mysite.com/iph/login.aspx?username=%#&password=%#",uname,pswd];
NSString *resultstr = [NSString stringWithContentsOfURL:[NSURL URLWithString:myurlstr] encoding:NSUTF8StringEncoding error:&error];
NSLog(#"HERE IS THE ONE: %#",resultstr);
Here is what the Console log prints:
TestingTrying[51514:207] HERE IS THE ONE: Invalid Login/Password
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head>title>
Untitled Page
/title>/head>
body>
form name="form1" method="post" action="login.aspx?username=abc&password=abc" id="form1">
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTnVSKZt+HQfSlQeCketelC9X+47A==" />
div>
/div>
/form>
/body>
/html>
Can someone help on how to get the string only(Invalid Login/Password) and not the content that starts from DOCTYPE onwards?
If your simply asking how to get the first line from your server response, use a tokenizer. This should point you in the right direction, just use new line which is /n from memory as the delimiter.

Question about regular expression evaluation using RegexKitLite

I am trying to get all css link from html like this segment of code:
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css"/>
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css" rel="stylesheet" type="text/css" />
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css" rel="stylesheet" type="text/css" />
Here is my code:
-(void)matchCSS:(NSString *)html{
NSString *regexString = #"href=\".*\.css\"";
NSArray *matchArray = NULL;
matchArray = [html componentsMatchedByRegex:regexString];
NSLog(#"matchArray: %#", matchArray);
}
However, what I got is a little bit crazy:
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css\" rel=\"stylesheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css\" rel=\"stylesheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css\" rel=\"stylesheet\" type=\"text/css\""
These are not pure link, some of them contains some other tags that I don't want. I didn't see anything wrong with my RE. Any suggestion?
The problem is with the .*, which is too greedy.
You should match every character that is not the quote character. I am not familiar with the regular expression syntax used by RegexKitLite, but I think the regular expression should be something like #"href=\"[^\"]*\\.css\"".
You should probably use a group; in that way, the function would return you only the characters included in the group, and not all the characters matching the regular expression. If I am not wrong, the regular expression should be something like #"href=\"([^\"]*\\.css)\"", in this case.