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?
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.
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.
The <input type="tel"> defines a field for entering a telephone number.
<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.
It is possible to implement HTML-only data validation on input
s 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With