but, I want create a single textbox where it work both attr(type =tel/email). means this is my jsFiddle https://jsfiddle.net/saidulu401/3y4kcqsj/4/ here the code I show as I want to create but I am familiar with angular and my website is on jQuery fully.
How I convert my jsfiddle to jQuery.
Please help to make code
Hi @HermioneGranger, if you want to keep the validation constraints in the markup you can just check the validity state of the element:
<!--
Note the `novalidate` attribute on the form -- without it,
the native browser validation will kick in as well.
-->
<form novalidate>
<ul id="errors">
<li>Please enter an email or a telephone number</li>
</ul>
<input type="text" name="emailphone" id="emailphone" pattern="^(?:\d{10}|\w+@\w+\.\w{2,3})$" />
<button>Submit</button>
</form>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$('#emailphone').on('keyup', function() {
var mismatch = this.validity.patternMismatch
$('#errors').toggle(mismatch)
$('button').prop('disabled', mismatch)
})
</script>
Hope you don’t mind me borrowing your markup @James_Hibbard… :-)
However, as @James_Hibbard noted validating an email “manually” is notoriously error-prone – see this classic thread on SO. I think there’s an npm module that just exports this regexp (from that very thread), but I can’t find it ATM… anyway it’s something I wouldn’t want to be responsible for myself really. ^^
A somewhat hacky but more robust solution might be to create an email input in-memory only, sync it with the actual input value and use that for the email-part of the validation:
<form novalidate>
<ul id="errors">
<li>Please enter an email or a telephone number</li>
</ul>
<!-- Only use the (also rather questionable) phone validation pattern here -->
<input type="text" name="emailphone" id="emailphone" pattern="^\d{10}$" />
<button>Submit</button>
</form>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
var email = $('<input />', {
type: 'email'
}).get(0)
$('#emailphone').on('keyup', function() {
email.value = this.value
var mismatch = (
this.validity.patternMismatch &&
email.validity.typeMismatch
)
$('#errors').toggle(mismatch)
$('button').prop('disabled', mismatch)
})
</script>
Indeed. I normally just check for <anything>@<anything>.<anything>. If you need to know that someone really owns the address they have entered, send them a mail with a confirmation link.