-2

HTML:

<form name="myForm" onsubmit="return validateForm();" method="post" >
    <input type="text" id="website" name="website" class="form-control" placeholder=" Website Link" >
    <button value="Save" name="register" class="btn btn btn-primary btn-block " type="submit" >Register</button>
</form>

JavaScript:

function validateForm(){
    var valdiationWebsite = document.forms["myForm"]["website"].value;
    if (valdiationWebsite == ""){
        alert("Please Enter Website Link ");
        return false;
    }
}

It will show an alert when field is blank.

But, if want to know is, entered text in the input field is valid website url or simple text, then what can i do?

How can i validate the entered text?

Please help me!

Dipak
  • 117
  • 1
  • 9

3 Answers3

1

The url type is used for input fields that should contain a URL address.

The value of the url field is automatically validated when the form is submitted.

The required attribute is a boolean attribute.

When present, it specifies that an input field must be filled out before submitting the form.

Try this

<input type="url" required id="website" name="website" class="form-control" placeholder=" Website Link" >
Sridhar R
  • 19,414
  • 6
  • 36
  • 35
0

try this regex /(ftp|http|https)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!-/]))?/

0

type=url may not support in all browsers. You can use javaScript to validate this.

function isValidUrl(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

Pass the user input to this isValidUrl function. Refer this stackoverflow question for better regualr expressions.

Community
  • 1
  • 1
Subin Jacob
  • 4,078
  • 6
  • 32
  • 65