i am developing application using Code-igniter.
I want to validate Text Box Using J Query Or JavaScript That only allowed to input following URL when user submit form.
http://
https://
ftp://
ftps://
file://
market://
linkedin://
fb://
geo:
maps://
Is there any way to do this ?
function is_url(str)
{
regexp = /^(?:(?:https?|ftps?|file?|market?|linkedin?|fb?|maps?):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
if (regexp.test(str))
{
return true;
}
else
{
return false;
}
}
Try using this regex in a function that checks validity of your input.
/^(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//g
But do the reverse logic:
if it matches then display error mesage and clear input.
If it not matches then your URL is fine
If you wanna prevent URLs anywhere in your input, remove the leading ^ in regex to match even if URL is not start of the string.
like: /(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//gm
Here is a crud version of entire solution:
fiddlejs
Main thing is URL validation:
$("#urlInput").focusout(function (){
var inputElement = $("#urlInput");
var regexp = /(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//gm;
if (regexp.test(inputElement.val())){
inputElement.val("");
alert("This is nto a valid URL")
}
});
It bound to the
<input id="urlInput" type="url">
You might wanna rework the error reporting in a more user friendly manner but that's another story.
Related
I want to block certain domains from input url field, i'm using jQuery Validation plugin, i've created a custom method only thing I can't figure out is the regex, it is driving me crazy.
$.validator.addMethod("customurl",
function(value, element) {
return /^([\w-.]+#(?!facebook\.com)(?!twitter\.com)(?!instagram\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value);
},
"Social links not allowed"
);
current regex blocks email addresses ending with those domains, but i want to block the urls, e.g:
https://www.facebook.com/username
https://facebook.com/username
http://facebook.com/username
I would just add another regex test using:
/https?\:\/\/(www\.)?facebook\.com\/(.*)/
Which might look something like:
$.validator.addMethod("customurl",
function(value, element) {
var emailTest = /^([\w-.]+#(?!facebook\.com)(?!twitter\.com)(?!instagram\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value);
var urlTest = /https?\:\/\/(www\.)?facebook\.com\/(.*)/.test(value);
return !emailTest && !urlTest;
});
I have an input form where users can fill out a profile for their internal tool that they are submitting. There is a link field that allows the user to enter a link to this tool.
The following formats are accepted:
http://
https://
file://
\\server\path\to\file.txt
W:\
I am trying create a javascript function to validate the input to make sure it is in one of the above mentioned formats.
I have found some generic URL validation regEx strings but none of them seem to include the local paths of files.
Here is a quick example of a function that works just fine for the top 2 needed formats but when it comes to network paths, I can't find anything that I can get integrated into one easy function.
var message;
var myRegExp =/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
var urlToValidate = "\\server.domain.com\path\file.txt";
if (!myRegExp.test(urlToValidate)){
message = "Not a valid URL.";
}else{
message = "Its a valid URL.";
}
alert(message);
JS Fiddle: https://jsfiddle.net/carlhussey/ebg4m278/
I am looking for a solution to validate the standard web protocols as well as network/file shares.
I need to validate a URL field for an input field that deals with a finicky API that will accept URL strings in a very certain format - no www's and no http's attached to the start of the string, or it breaks. A javascript handler grabs the data from the validation.php file. I have everything working except for one field.
So, for example I want
example.com/example
and
example.com
to pass validation, but I want
http://example.com/example
and
http://www.example.com/example
and any variation including any derivation of http:// or www. to not validate.
The regex I have below currently allows any type of URL including http and www. I want it to ONLY allow the above type of URL string, but not sure how to go about this.
// API URL VALIDATION
if ($formfield == "api_url") {
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i", $value)) {
echo "Please enter your URL without the http://www part attached"
} else {
echo "<span>Valid</span>";
}
}
Any ideas?
You can use this simple negative lookahead based regex in preg_match:
~^(?!https?://)(?!www\.).+$~i
RegEx Demo
You can try to "not match" a subject that begins with http or www:
if(!preg_match('/^((http|ftp)s?:\/\/)?(www\.).+?/', $value))
You can do it with .htaccess mod rewrite rule. But in your case it looks like you need to validate a input field so it doesn't apply to all cases. Here is function which will allow/dis-allow such protocols in the url.
function remove_protocol($url) {
$filter_protocol = array('http://', 'https://', 'http://www');
foreach($filter_protocol as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}
in the $filter_protocol array you can keep on defining protocols not to allow in urls for instance ftp://
I know it may sound as a common question, but I have different variables here:
I have an Url like this one:
https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5
Now I need to get the number 546604752058417. So logically I should get all what is after events/ but all what is after the first slash after 546604752058417
Now the problem is that the Url may or not start with http, https, www, etc..
I am a little lost and new to Javascript.
What's the simple way to do it?
I have a function that check if the Url is valid and it is from Facebook, but I don't know now how to get that 546604752058417.
Obviously this is only a sample. The function should be able to work with any event id.
function fbProcessSearch() {
var search_url = $("#search_fb_url").val();
if(isFbUrl(search_url)) {
$("#search_fb_event").validationEngine("hide");
// gets the event id from the Url and passes it to the below function.
fbProcess(eid);
}else{
$("#search_fb_event").validationEngine("showPrompt", "Please enter the Facebook event Url", "load", "topLeft", true);
}
}
function isFbUrl(url) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
return regexp.test(url) && url.indexOf("facebook.com") > -1;
}
Use .match with this regexp: /events\/(.*?)\//
"https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5".match(/events\/(.*?)\//)[1]
=> "546604752058417"
Explanation:
events\/ -> Match "/events/" literally
(.*?)\/ -> match anything until first "/" and also capture it
if you know how the url is going to look and can confirm it's facebook. then .split("events/")[1].split("/")[0] to get the code between /events/######/
you can start by "spliting" your url with "/"
var search_url = $("#search_fb_url").val().split("/");
and then you take the field after the one containing "events" ....
I tried to write a regular expression to work with the position-absolute jQuery validation plugin to give error if the string is left empty OR its a invalid URL. It works well for the URl but doesn't give error if a empty string is there . Here's the regex
"regex": /^\S$|^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/,
Can somebody tell what's wrong in the regex?
EDIT:
I'm using this plugin for jQuery validation
Demo: Go to this URL and in the URL validtion section, remove HTTP and see the error messages
http://www.position-relative.net/creation/formValidator/demos/demoValidators.html
When I add required and URL validators together, it shoots 2 errors. But with only URL
validator, it doesn't say that its a Invalid URL. I just need 1 error for both Empty and
Invalid URL.
https://github.com/posabsolute/jQuery-Validation-Engine
Thanks
You can set the same generic error message for all errors using data-errormessage, so regardless of whether the field is empty or has an invalid value you will get the same message.
have you tried jQuery URL validation metohod ?
I wrote a small example how you could validate a email realy quick and easy. Without the hassle of using a plugin. If you use it just once you could just use
var emailaddressVal = 'nobody#example.cpom';
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
if(emailReg.test(emailaddressVal) && emailaddressVal.length > 0)
{
alert('Your email is valid.');
}
else
{
alert('The email is invalid')
}
However putting it in a function so you could use it multiple times. than put it in a function for example:
// Email check function
function checkEmail(email)
{
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
if(emailReg.test(email) && email.length > 0)
{
return true;
}
else
{
return false;
}
}
// Ussage
if(checkEmail('nobody#example.com'))
{
alert('SUCCESS!!');
}
else
{
alert('FAILED!!');
}