This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the best regular expression to check if a string is a valid URL?
I want to validate text box by regular expression. It should be Null or valid Domain Name Like Google.com, example.com.
Thx
Something along the following lines will work.
function isValidURL(url)
{
return url.match(/([a-zA-Z]+:\/\/)?(.+\.)?(\w+)(\.\w+)(:[0-9]+)?(\/.+)?/) ? true : false;
}
alert(isValidURL(document.getElementById('textBoxId').value));
It won't work for things like "example.com" as really these aren't "valid" urls in that sense. A valid url beings with http://
Related
This question already has answers here:
Check whether a string matches a regex in JS
(13 answers)
Closed 8 months ago.
Hi I am hoping this may be and easy one for some of you.
Essentially I am just asking if it is possible to use a regex match statement within a if statement.
I have used some in my Formik validation schema but am not sure if it is possible to use within an if statement.
This is my if statement
if (this.state.email.length < 8 || this.state.password.length < 8)
I would like to include something along the logic of
.matches(/(?=.*outlook)/)
Is this possible ?
I think what you're looking for is the regex .test() method. It applies a regex to a string and returns true if it matches, or false if it doesn't. See here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
you can use match to validate regex expression in javascript, this is an example of email validation :
function ValidateEmail(inputText) {
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (inputText.match(mailformat)) return true;
}
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 6 years ago.
I have created a url field and submit button.and i want to validate this url like that it will show .xml regular expression at the end of url instead of .com and anything else. I have tried this code but didn't get reliable solution.It's redirecting me on another page when i writes invalid url.
Please help me through this . Any help will be appreciated.
Thanks in advance.
function validateURL(){
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/.xml;
if (!re.test(url)) {
alert("url error");
document.getElementById("error_msg").innerHTML = "invalid url";
return false;
}
}
Use this RegEX
/(https?!:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/
This question already has answers here:
JavaScript regular expression validation for domain name?
(5 answers)
Closed 7 years ago.
I want to validate domain name (only), which is going to be entered into textbox.
Acceptance Criteria:
abc.com
abc.in
abc.pqr.com
abc.com/xyz
abc.com/xyz/pqr
no.matter.how-many.domain-name.com/anything
Reject:
http://example.com
(anything preceding with http:// or https:// or ftp://)
abc.com////
abc.com///xyz
.....likewise
I have/had tried this:
^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.?)^[a-z0-9]?$/i
But it doesn't take: abc.com/xyz or abc.com/ (that is in my acceptance criteria)
(After that)Also tried this:
/([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
This overcome above problem,But it also take like: http://example.com (that is in my reject criteria)
I am not more good at Regular expressions, so, what will be the regular expression for above scenario.
(Any modifications do i need in existing code?)
This solves my problem:
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})(\/(?:[\/\w \.-]*)*\/?)?([\/?#].*)?
This Article helps.
Edit: Not Working. Also accepting ..com and example.com/////abc
Try this:
/^[\w\-]+\.\w[\w\-\.]*((\/\w+)*|\/)$/igm
Regex Demo
This was the best I could come up with. Didn't really check it too much
^((?:[^:\/\.\s]+\.)+[^:\/\.\s]+(?:(?:\/[^:\/\.\s]+)*))$
regex101
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular expression for URL validation (in JavaScript)
How to validate url?
I have a textbox in a form where user can enter any website url. i want to check that url, whether it is valid or not through regular expression. it will accept only like this "http://www.google.com" or "www.google.co.in".
I have using a expression like
/((http|https|ftp):\/\/(www|WWW)|(www|WWW))\.[A-Za-z]{3}/ .
it is working but when i enter wwww.google.co.in, it says valid.
can any one help me please.
Do a request server side and check for 200 OK. Don't trust the client.
We needed it in our project a couple of weeks ago, and I think we used this one
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
return regexp.test(s);
}
UPDATE:
I'm still working on it:
(ftp|http|https):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+#&#;`~=%!]*)(\.\w{2,})?)*\/?
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Checking for a valid url using Javascript Regular Expressions
PHP validation/regex for URL
I have a if statement that will check if the user entered a URL(HTTP Protocol only), like this:
if(/^regexp/.test(url))
But how should be this regular expression to check if the text is a URL or not?
I believe this little function might help:
function isURL(string){
regEx = /(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim;
return regEx.test(string));
}
Let us know if it worked out!
W.