I was trying to get the <input type="email" name="mail">
's value from jQuery
.
I've to run a validation for checking mail
's value
<script>
function valid(){
var em = $.trim($('input:email[name=mail]').val());
if(em.length < 5){
alert('Please enter email id');
return false;
}
return true;
}
</script>
but the form is still submitting if the user left mail
blank.
I've gone through jQuery API Doc but not found any selector
for email
. Why so?
Why it is not present there and how would I solve this problem?
I see that this is rather old, but I would like to throw my two cents in for the future generations.
While there is not strictly a pseudo selector like input:email
, there is a type-selector version that works just like the pseudo would: input[type=email]
Extra reading:
this selector can do alot of cool things like selecting all that starts with an id: input[id^=prod-]
would select all inputs with an id of prod-X where X could be anything and any length. This is achieved using ^=
you could do:
$.trim($("input[name='mail']").val());
Use this code to get date submitted through form, this will work perfectly fine:
$("input[type = 'date']").val();
In case it ever helps anyone, I had two inputs with same id, but one for mobile and one for desktop. I was testing the desktop and getting no value. Turns out the mobile one although hidden was the input getting selected, thus returning blank.
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