Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make an input="text" to require either an email or a tel?

I made an <input type="text" placeholder="phone or email" required="required" /> and I was just wondering if I can make it so that it would require either a valid email or a number?

like image 830
Pa3k.m Avatar asked Jul 27 '15 02:07

Pa3k.m


People also ask

What type the input fields has to be to contain an email address?

The <input type="email"> is used for input fields that should contain an e-mail address. Depending on browser support, the e-mail address can be automatically validated when submitted.

How do you make an input only accept a number?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

What is input type TEL in HTML?

The <input type="tel"> defines a field for entering a telephone number.

Is there an input type email?

<input type="email"> <input> elements of type email are used to let the user enter and edit an e-mail address, or, if the multiple attribute is specified, a list of e-mail addresses.


1 Answers

It is possible to implement HTML-only data validation on inputs using pattern attribute: http://jsfiddle.net/887saeeg/. Coupled with :valid and :invalid pseudo-classes, it is possible to have a decent error-checking functionality using only presentation technologies. Of course, a modern browser is required. (I cover browser-only validation in more detail in the last volume of my Functional CSS book series [available on Amazon]).

HTML:

<form>
    <input type = "text" placeholder = "Phone or Email" required pattern = "^([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3})|(\d{3}-\d{3}-\d{4})$"/>
    <input type = "submit" value = "Send" />
</form>

EDIT: Example using CSS pseudo-classes: http://jsfiddle.net/292pp5gk/.

HTML:

<form>
    <label>
        <input type = "text" placeholder = "Phone or Email" required pattern = "^([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3})|(\d{3}-\d{3}-\d{4})$"/>
        <span class = "error">Please provide a valid telephone or email</span>
    </label>
    <input type = "submit" value = "Send" />
</form>

CSS:

label {
    position: relative;
}

.error {
    position: absolute;
    white-space: nowrap;
    bottom: -8px;
    left: 0;
    transform: translateY(100%);
    display: none;
    background-color: hsla(0, 50%, 70%, 1);
    padding: 5px 10px;
    border-radius: 5px;
    font: normal 12px/1 Sans-Serif;
}

.error:before {
    content: "";
    position: absolute;
    border-style: solid;
    border-color: transparent transparent hsla(0, 50%, 70%, 1) transparent;
    border-width: 0 5px 5px 5px;
    left: 15px;
    top: -5px;
}

input {
    outline: 0;
}

input:invalid + .error {
    display: block;
}
like image 190
DRD Avatar answered Sep 28 '22 02:09

DRD