Detect if Asp.Net form is valid with javascript/jquery

℡╲_俬逩灬. 提交于 2019-12-05 09:23:11

Okay I found a solution. Page_ClientValidate() does the trick.

function showLoading() {
    if (Page_ClientValidate()) { $("#loader").show(); }
        else { alert("Form is invalid!"); }
    }

I think it would be better to use the property Page_IsValid instead of the method Page_ClientValidate().

function showLoading() {
if (Page_IsValid) { $("#loader").show(); }
    else { alert("Form is invalid!"); }
}

If you don't want to call Page_ClientValidate() you can use Page_IsValid but set a timeout so that the validation occurs first.

OnClientClick="javascript:setTimeout(function() { if (Page_IsValid) document.getElementById('preloader').style.display='block';}, 1000);"

the preloader is hidden in the middle

<img src="/images/preloader.gif" id="preloader" style="display:none; position: fixed; top: 50%; left: 50%; margin-top: -50px; margin-left: -100px;" />

Thank you Hasan, you made my day :)

I want to share some bit of my problem and resolution. I have an ASP.NEt page using a MasterPage. When submiting a form I used the fadeOut effect to hide the buttons. However if the form is not completed the buttons was gone. I simply add a condition on hasan suggestion to the OP problem. Here is my final code.

//Fade out buttons when clicked but only if page validate
$('.button').click(function () {
    if (Page_ClientValidate()) { $('.button').fadeOut('slow'); }
});//End of Fade out buttons

Thanks again Hasan :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!