Javascript验证文本区域以查找SSN(Javascript validating a text area to find a SSN)
我正在尝试创建一个文本区域备注字段的快速验证,以查看它是否可能包含一个SSN号码,然后我将为此发出警报。
我已经尝试了几种不同的RegEx模式,我在网上找到它们,但它们都没有工作。 我想知道如果它的JavaScript是不正确的?
我相信,如果它找到一个匹配,那么将会是真的,因此抛出
SSN Found
警报。有人指出我的错误?
$(document).ready(function() { $('[name="submit"]').click(function() { validate(); }) }) // Validate our note field function validate() { var isValid = true, notes = $('[name=notes]').val(), ssn = new RegExp('^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$'); console.log(notes) if (ssn.test(notes)) { alert('SSN Found'); }else{ alert('No SSN Found'); } }
JS小提琴: https : //jsfiddle.net/sgrw4rqf/2/
I am trying to create a quick validation of a text area notes field to see if it potentially contains a SSN number which I will then throw an alert for.
I have tried a few different RegEx patters I have found online and none of them seem to be working. I am wondering if its my javascript that isn't correct?
I believe if it were to find a match, it would be true thus throw the
SSN Found
alert.Anyone point out my mistake?
$(document).ready(function() { $('[name="submit"]').click(function() { validate(); }) }) // Validate our note field function validate() { var isValid = true, notes = $('[name=notes]').val(), ssn = new RegExp('^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$'); console.log(notes) if (ssn.test(notes)) { alert('SSN Found'); }else{ alert('No SSN Found'); } }
JS Fiddle: https://jsfiddle.net/sgrw4rqf/2/
原文:https://stackoverflow.com/questions/49565090
最满意答案
从@shieldstroy发布的使用Great Circle Distance发布的问题中得到这个答案的算法,我得到了这个例子的工作。
这里是
Comparator
:public class SortPlaces implements Comparator<Place> { LatLng currentLoc; public SortPlaces(LatLng current){ currentLoc = current; } @Override public int compare(final Place place1, final Place place2) { double lat1 = place1.latlng.latitude; double lon1 = place1.latlng.longitude; double lat2 = place2.latlng.latitude; double lon2 = place2.latlng.longitude; double distanceToPlace1 = distance(currentLoc.latitude, currentLoc.longitude, lat1, lon1); double distanceToPlace2 = distance(currentLoc.latitude, currentLoc.longitude, lat2, lon2); return (int) (distanceToPlace1 - distanceToPlace2); } public double distance(double fromLat, double fromLon, double toLat, double toLon) { double radius = 6378137; // approximate Earth radius, *in meters* double deltaLat = toLat - fromLat; double deltaLon = toLon - fromLon; double angle = 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(fromLat) * Math.cos(toLat) * Math.pow(Math.sin(deltaLon/2), 2) ) ); return radius * angle; } }
这是高级代码,我只是把它放在
onCreate()
://My location, San Francisco double lat = 37.77657; double lng = -122.417506; LatLng latLng = new LatLng(lat, lng); //set up list ArrayList<Place> places = new ArrayList<Place>(); places.add(new Place("New York", new LatLng(40.571256,73.98369))); places.add(new Place("Colorado", new LatLng(39.260658,-105.101615))); places.add(new Place("Los Angeles", new LatLng(33.986816,118.473819))); for (Place p: places){ Log.i("Places before sorting", "Place: " + p.name); } //sort the list, give the Comparator the current location Collections.sort(places, new SortPlaces(latLng)); for (Place p: places){ Log.i("Places after sorting", "Place: " + p.name); }
这是日志输出:
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: New York 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Colorado 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Los Angeles 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Los Angeles 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Colorado 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: New York
Taking the algorithm from this answer from the question posted by @shieldstroy, that uses the Great Circle Distance, I got this example working.
Here is the
Comparator
:public class SortPlaces implements Comparator<Place> { LatLng currentLoc; public SortPlaces(LatLng current){ currentLoc = current; } @Override public int compare(final Place place1, final Place place2) { double lat1 = place1.latlng.latitude; double lon1 = place1.latlng.longitude; double lat2 = place2.latlng.latitude; double lon2 = place2.latlng.longitude; double distanceToPlace1 = distance(currentLoc.latitude, currentLoc.longitude, lat1, lon1); double distanceToPlace2 = distance(currentLoc.latitude, currentLoc.longitude, lat2, lon2); return (int) (distanceToPlace1 - distanceToPlace2); } public double distance(double fromLat, double fromLon, double toLat, double toLon) { double radius = 6378137; // approximate Earth radius, *in meters* double deltaLat = toLat - fromLat; double deltaLon = toLon - fromLon; double angle = 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(fromLat) * Math.cos(toLat) * Math.pow(Math.sin(deltaLon/2), 2) ) ); return radius * angle; } }
Here is the high level code, I just put this in
onCreate()
://My location, San Francisco double lat = 37.77657; double lng = -122.417506; LatLng latLng = new LatLng(lat, lng); //set up list ArrayList<Place> places = new ArrayList<Place>(); places.add(new Place("New York", new LatLng(40.571256,73.98369))); places.add(new Place("Colorado", new LatLng(39.260658,-105.101615))); places.add(new Place("Los Angeles", new LatLng(33.986816,118.473819))); for (Place p: places){ Log.i("Places before sorting", "Place: " + p.name); } //sort the list, give the Comparator the current location Collections.sort(places, new SortPlaces(latLng)); for (Place p: places){ Log.i("Places after sorting", "Place: " + p.name); }
Here is the log output:
04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: New York 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Colorado 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places before sorting﹕ Place: Los Angeles 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Los Angeles 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: Colorado 04-17 23:04:16.074 12963-12963/com.maptest.daniel.maptest I/Places after sorting﹕ Place: New York
相关问答
更多-
使用谷歌API作为 public float getDistance(double lat1, double lon1, double lat2, double lon2) { String result_in_kms = ""; String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + l ...
-
Location loc1 = new Location(""); loc1.setLatitude(lat1); loc1.setLongitude(lon1); Location loc2 = new Location(""); loc2.setLatitude(lat2); loc2.setLongitude(lon2); float distanceInMeters = loc1.distanceTo(loc2); 参考: http : //developer.android.com/refe ...
-
Apple的地理编码对象不适用于批量地理编码。 有关更多信息,请参阅Xcode中的CLGeocoder类参考。 简短摘录: 应用程序应该意识到他们如何使用地理编码。 地理编码请求对每个应用都是速率限制的,因此在短时间内请求太多可能会导致某些请求失败 。 (当超过最大速率时,地理编码器会返回一个错误对象,并将值kCLErrorNetwork返回给关联的完成处理程序。)以下是有效使用此类的一些经验法则: 最多发送一个地理编码请求以进行任何一项用户操作。 如果用户执行涉及对同一位置进行地理编码的多个操作,请重用初 ...
-
从@shieldstroy发布的使用Great Circle Distance发布的问题中得到这个答案的算法,我得到了这个例子的工作。 这里是Comparator : public class SortPlaces implements Comparator
{ LatLng currentLoc; public SortPlaces(LatLng current){ currentLoc = current; } @Override ... -
我有几点建议: 1)要降低gps修复量,可以计算睡眠时间。 我们的想法是在靠近“边界”时进行频繁的GPS修复,而在远处使用时则很少。 通过考虑GPS返回的速度参数可以进一步增强这一点。 例如,如果用户距离最近的边界20英里,同时行驶20英里每小时,您可以轻松等待30分钟,然后再次打开GPS,只是举个简单的例子。 2)我遇到了同样的问题。 我的解决方案只是选择X英里内的POI。 这样确保永远不会冒险达到100的限制。当用户从其初始位置移动(X-1)英里时,使用新位置重建地理围栏。 如果您认为存在如此高密度点的 ...
-
如何在距离另一个地理点的距离d内创建随机地理点?(How to create Random Geo-Points within a distance d from another Geo-point?)[2022-01-11]
我刚写了一个Ruby方法,它扩展了Random来提供这个。 警告 :所有点都在一个盒子里,而不是一个圆圈。 class Random def location(lat, lng, max_dist_meters) 这是使用Random.new.location(mid_lat, mid_lng, dist)调用的。 它将返回一个可能在中间点的max_dist_meters内的点。 max_radius = Math.sqrt((max_dist_meters ** 2) / 2.0) 如果不 ... -
我终于想出了答案。 问题是距离函数被异步调用,结果总是为零。 我为forwardGeocoding函数创建了一个完成处理程序,以从地址字符串中返回经度和纬度,并调用嵌套的Firebase侦听器中的结果。 这里是代码,我希望如果有人遇到类似的问题,我会发现它有帮助。 //Get lat and long func getCoordinates(address: String, completionHandler: @escaping (_ lat: CLLocationDegrees?, _ long: ...
-
人力资源管理。 在cellForRowAtIndexPath中不应该发生排序。 现在已经太晚了。 如果每个单元格都是1 bar,并且条形应按距离排序,我建议您在self.barInfo的懒惰getter中进行排序(如果这是您的条形列表)。 我不确定你的模型是什么样的,但是如果每个bar对象都有一个与你的参考点之间的距离的属性,你的排序看起来就像你在那里那样。 这种排序只会在加载第一个单元格(即“懒惰”部分)然后进行缓存时发生。 所以你的cellForRowAtIndexPath:只需要在它的indexPat ...
-
这是一个解决方案。 由于以下几个原因,直接调用sort函数有点复杂:首先需要找到组织中最近的位置,如问题所述,其次是在CLLocation使用的hasrsine计算这一事实distanceFromLocation:如果天真地使用,方法会降低速度。 出于这些原因,我创建了一个特殊对象来进行排序,这样我就可以使用成员字典来记忆对distanceFromLocation的调用结果。 这不会对测试数据产生影响,但如果您需要处理大量的地方,则无关紧要。 作为旁注 - 如果OrganizationObject存储CLL ...
-
pin.location是包含要排序的位置的字段的名称。 如果pin是一个简单对象,即不是nested对象,则不需要nested_path 。 mode和sort_mode都是正确的名称,但是,不推荐使用sort_mode ,因此应首选mode 。 pin.location is the name of field containing the location to sort against. nested_path is not needed if pin is a simple object, i.e ...