How To Validate URL Using JavaScript
If You Are Creating A Webpage Having URL TextBox , Then Definitely You Need To Validate That The Entered String Is Valid URL Or Not. There Are Many Ways To Validate URL . In This Post , We Will Learn How To Validate URL Using JavaScript
URL Validation In JavaScript
Our Main Aim Is To Validate URL Using JavaScript. For This We Will Use RegEx (Regular Expression) In JavaScript To Validate URL.
Our Requirement In Valid URL
- URL Must Start With http:// or https://
- URL May Or May Not Contain www
- URL Must Not Be A Magnet URL (Type Of Torrent URL)
JavaScript Code For Validating URL
function checkforvalidurl() {
var thelink = document.getElementById("mylink").value;
var regEx = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
return regEx.test('https://www.google.com');
}
function isValidURL() {
if (checkforvalidurl() == true) {
alert("Valid URL");
}
else {
alert("InValid URL");
}
}
Change 'https://www.google.com' With The URL Which You Want To Validate Or Declare A Variable Which Contain A TextBox's Text And Replace 'https://www.google.com' With That Variable (Enter Variable Without Quotes). Call The Function isValidURL() To Validate The URL Using JavaScript
Post a Comment