Javascript Phone Number Autocomplete - javascript

Hi is there anyway using Javascript to get user's current location and autocomplete fields in a form like telephone country code (e.g. in Singapore it is +65) and area code?

It's really difficult, and I'd advise against it because it's loads of effort for very little reward. The best way (most fluid for the user) would be to get the users IP using PHP/ASP/Ruby-on-Rails/whatever and query one of the freely available Geo-IP databases (Google it) to match that to a country, then create your own database of country codes (using this, or similar).
If you really really want to do it using JavaScript, it'll only let you do it in newer browsers, and it'll prompt the user that you want to know their location... a lot of people will wonder why you're asking for this and click no.
// first, check it's supported
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(insertCountryCode);
}
insertCountryCode is a function that you pass. Why? Because the prompt for the user to allow your website to know their location is non-modal, meaning the page will carry on running while it waits for a response, so the function when they do respond will be asynchronous (outside the normal flow). This function is where you look up their location and insert their country code. In this function, use the Google Maps API and pass the latlng parameter to get the country... then you need to find another lookup service which will let you find out the dialing code.
The reason I've explained this is to show you how hard it is. A much simpler alternative would be to have a drop-down box with
<option value="+65">Singapore</option>
and insert the value into the phone number box. That's what I advise you do.

Related

Best way to use food database API

I'm currently working on a node/react application that will make use of a food database API like the one offered by the USDA.
My question is, how are these APIs best used in order to limit API calls?
For example, autocomplete, as a user is typing into the search field for a food, i want to give them a list of say 10-20 possible options based on what they typed. However, as they continue to refine what they type in, this is going to generate multiple API calls in order to return possible items. What is the best way to limit the amount of calls?
Is it better to somehow store the data locally? It's a massive database that the USDA has, I'm not sure it's feasible or even allowed to be copied necessarily.
Any other time I have dealt with APIs it has just been a call here or there but nothing like this and I guess I'm just confused on where to get started with it.

Two different google APIs on the same page

Is it possible to get two different javascript Google APIs to work on the same page? My goal is to use the autocomplete Google API to get an address to pass into the calculate distance API, allowing the user to autocomplete their address and immediately see the estimated travel time to a predefined location (similar to Uber). I am new to javascript, so I am not sure if there is some trick to solve this problem; any advice?
If the question is "is it possible ?", I could just answer "yes" :).
You can insert autocomplete in HTML that will lead subsequent call to distance API, no problem.

Run Database Stored RegEx against DOM

I have a question about how to approach a certain scenario before I get halfway through it and figure out it was not the best option.
I work for a large company that has a team that creates tools for the team mates to use that aren’t official enterprise tools. We have no access to the database directly, just access to an internal server to store our files to run and be able to access the main site with javascript etc (same domain).
What I am working on is a tool that has a ton of options in it that allow you to select that I will call “data points” on a page.
There are things like “Account status, Balance, Name, Phone number, email etc” and have it save those to an excel sheet.
So you input account numbers, choose what you need and then using IE Objects it navigates to the page and scrapes data you request.
My question is as follows..
I want to make the scraping part pretty Dynamic in the way it works. I want to be able to add new datapoints on the fly.
My goal or idea is so store the regular expression needed to get the specific piece of data in the table with the “data point option”.
If I choose “Name” it knows the expression for name in the database to run again the DOM.
What would be the best way about creating that type of function in Javascript / Jquery?
I need to pass a Regex to a function, have it run against the DOM and then return the result.
I have a feeling that there will be things that require more than 1 step to get the information etc.
I am just trying to think of the best way to approach it without having to hardcode 200+ expressions into the file as the page may get updated and need to be changed.
Any ideas?
IRobotSoft scraper may be the tool you are looking for. Check this forum and see if questions are similar to what you are doing: http://irobotsoft.org/bb/YaBB.pl?board=newcomer. It is free.
What it uses is not regular expression but a language called HTQL, which may be more suitable for extracting web pages. It also supports regular expression, but not as the main language.
It organizes all your actions well with a visual interface, so you can dynamically compose actions or tasks for changing needs.

Is it possible for me to prevent hackers from using a web console to call an upvote (AJAX) function repeatedly with JavaScript?

I recently created a website that has a voting/upvoting feature that uses jQuery's AJAX functions. The catch is: anyone can vote. I don't require visitors to be logged in, I don't track their IP, and I don't even store a long-term cookie. Normally (don't laugh), when a user votes on something, I store the ID of the item they vote for in a JavaScript array. Whenever they try to vote, the script checks if they have voted for the given item recently by checking the array for the ID. If they have, it just gives them an alert dialog. Otherwise, it casts a vote. So it goes without saying that all a user has to do to vote again is refresh the page.
I decided to see what happens if I injected some JavaScript (in the URL bar or a web console), and I wasn't really surprised to find out that voting as many times as you want very rapidly is as easy as:
for (var i = 0; i < 100; i++) { vote(itemID); }
(and that's being nice). I'm not sure why the array isn't stopping it, but that doesn't matter; it will always be easy to exploit this, right? I mean - you could even write a little HTML document with some JS that calls the voting page on my website as many times as you want.
So I want to fix this without too much trouble. Is it possible to create an immutable variable in JavaScript? A constant (though it would really help if there was such thing as a constant that could be changed only once)? The easiest way to fix this to some degree would be to keep the ID-holding array semi-constant: can't be deleted, but can be added to. Any suggestions or solutions or explanations are greatly appreciated.
No, if the users want to cheat by messing with their client-side code, they can do that. This is just like cheat patches for games.
What you are doing now is very reasonable to avoid accidental duplicate votes, if you feel that this is not enough (for example if the votes are really important), you need to take measures on the server-side. Tracking IP or setting cookies also won't work, this relies on client-side cooperation as well. You'd have to authenticate the users and mave sure everyone votes only once by storing something in your database.
I don't know why you don't track the IP, if you track the IP, then you can check if the IP voted the same item before, or if the IP voting too often.
Of course you also need to take into consideration that people might share the same IP to browse to your site.

JavaScript autocomplete upon typing

Right guys, all autocomplete plugins and functions that I've found, they only update upon keyup/down, etc. This is fine but the search only begins occurring once the user has stopped typing and if they are typing a phrase or word, the script is unable to instantly start suggesting, etc.
I know this'll be a very simple fix or suggestion for some of you guys, so any help would be greatly appreciated as to how I can convert it to be instantly as a key is pressed.
An example of the desired effect is Google Suggest or Facebook search, a search is fired instantly per key press or change, how do I emulate this?
Thanks!
Is this what you mean? Or do you want Ajax to retrieve from a database?
var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
$("#example").autocomplete(data);
JQuery
Edit: I'm not sure I know what you mean, because this example seems to work identical to Google Suggest or Facebook. If your database was small you could download the cache into the variable data upon page load. If your database was slightly larger you LIMIT the cache to only X number of responses for each alphabetical character or series of characters. (ie. WHERE city LIKE 'aa%' LIMIT 10 AND WHERE...)
It depends on how big the space you're searching is and how good your servers are. Facebook search for (I assume people's names) is quick because you're only really searching through a thousand or so contacts. Google is fast because they invest a lot of money in infrastructure and cache a lot of the responses.
On one of my projects I've used this jQuery plugin and it provides excellent performance on cached results. We used it to provide autocomplete functionality on a list of about 6K contacts (names, etc). Is this what you had in mind?
The Wicket web framework has the concept of a "throttling" behavior. Normally, AJAX requests in Wicket applications are queued against an "ajax channel", which triggers a request instantly if none is running. If a request is already running, the next request is queued, and triggered when the current one returns.
"Throttling" lets the behavior delay itself for a certain amount of time (say, two seconds). If the behavior fires again in the same period, the callback for the most recent behavior replaces the callback for the current queued behavior. (For example, the user starts typing "albuquerque", which triggers the events "A" then "AL", then "ALB". The system might trigger "A", then "ALB", skipping over "AL" because it was replaced by "ALB" while sitting in the queue.) The object of this is to fire a behavior instantly on each keypress, but prevent the server from being flooded with unnecessary requests.
Check out the wicket ajax source code:
https://github.com/apache/wicket/blob/wicket-1.4.8/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
For more about the web framework, see:
http://wicket.apache.org

Categories

Resources