Email validation doesn't capture with required fileds

时间:2015-07-29 00:19:28

标签: javascript c# jquery asp.net-mvc

I have a javascript validation function.I need to check if required fileds are empty or wrong mail address.Required fileds empty is working But when i type mail like abc@abc or something wrong then it doent catch the error in my code.

When i type all required fileds but wrong email address ( abc@abc or abc.com like doesn't capture.)

My Code

<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

Email Validate Function

 function newsValidation() {

    var status = true;
    if (($.trim($('#txtNewsname').val()) == '') || ($.trim($('#txtnewsarea').val()) == '') ||
        ($.trim($('#txtemail').val()) == '')) {

        $("#reqfield").removeClass("hidden");

        if (!ValidateEmail($("#txtemail").val())) {
            $("#emailval").removeClass("hidden");
        }
        status = false;
    }

1 个答案:

答案 0 :(得分:1)

Your test for a valid email is inside the :checked => this.first block which test if the value is not if, so when you enter any value in the text box (whether its valid or not) the null will never be called. Change your script to

if (!ValidateEmail($("#txtemail").val())) {

Side note: All this functionality is provide out of the box in MVC by simply adding the function newsValidation() { var status = true; if (($.trim($('#txtNewsname').val()) == '') || ($.trim($('#txtnewsarea').val()) == '') || ($.trim($('#txtemail').val()) == '')) { $("#reqfield").removeClass("hidden"); status = false; } else if (!ValidateEmail($("#txtemail").val())) { $("#emailval").removeClass("hidden"); status = false; } } and [Required] attribute to your property and including the relevant scripts ([EmailAddress] and jquery.validate.js) and jquery.validate.unobtrusive.js helpers which means you get both client and server side validation (and it's all done correctly!)