Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get input type email's value from jquery

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?

like image 615
Mohammad Faisal Avatar asked Aug 26 '13 11:08

Mohammad Faisal


4 Answers

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 ^=

like image 52
Troels M. B. Jensen Avatar answered Oct 27 '22 10:10

Troels M. B. Jensen


you could do:

$.trim($("input[name='mail']").val());
like image 24
Sudhir Bastakoti Avatar answered Oct 27 '22 08:10

Sudhir Bastakoti


Use this code to get date submitted through form, this will work perfectly fine:

$("input[type = 'date']").val();
like image 26
Junaid Asmat Avatar answered Oct 27 '22 09:10

Junaid Asmat


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.

like image 26
AdamVanBuskirk Avatar answered Oct 27 '22 10:10

AdamVanBuskirk