ボタンのクリックでユーザー情報を保存し、ページをリダイレクトする Web ページに取り組んでいます。
ただし、ユーザーがコントロールの 1 つに値を入力するのを忘れた場合に、ユーザーに警告したいと考えています。エラーを修正するように依頼し、もう一度保存ボタンをクリックします..
私は確認ボックスで同様の機能を実装しましたが、アラートボックスには戻り値がないため、そのような実装が可能かどうか疑問に思っていましたか?
ボタンのクリックでユーザー情報を保存し、ページをリダイレクトする Web ページに取り組んでいます。
ただし、ユーザーがコントロールの 1 つに値を入力するのを忘れた場合に、ユーザーに警告したいと考えています。エラーを修正するように依頼し、もう一度保存ボタンをクリックします..
私は確認ボックスで同様の機能を実装しましたが、アラートボックスには戻り値がないため、そのような実装が可能かどうか疑問に思っていましたか?
Instead of going to validate with custom JavaScript and alert boxes, try to reuse the standard ASP.net validation controls. There are a bunch of them available for required fields, comparison validators, custom validators etc. The advantage is that you use standard mechanisms and that you can access them for doing server-side as well as client-side validation. The server-side validation is important if your user has JavaScript disabled in his browser which still seems to happen.
Moreover I actually prefer that things happen directly on the page and not with alert boxes (this is somehow the old way of doing things). Alert boxes lower the user experience since he continuously has to click it away and moreover he has to read what happened, while if the error is shown directly beneath its source makes it easier to see (just from a usability point of view).
You could alternatively also look at the jQuery validation mechanism. As far as I know there exists one.
サーバ側
<asp:Button ID="MyButton" runat="server" OnClientClick="return myFunction();" OnClick="MyButton_Click" Text="Save" />
クライアント側
function myFunction() {
if (document.getElementById("<%=this.MyFieldName.ClientID%>").value.length == 0) {
alert('Please fill in the field.');
return false;
}
return true;
}
また、asp:RequiredFieldValidator タグを使用して、ASP.Net 検証ロジックにすべてのチェックとユーザー プロンプトを実行させることもできます。
戻り値は必要ないと思います。関数は、false を返すだけでボタンの送信をキャンセルできます。
function myBtnClick()
{
if (document.getElementById("myTextBox").value == "")
{
alert("You forgot to fill it in!");
return false;
}
return true;
}
または、アラート ボックスのテキストを質問に変更し、代わりに確認ボックスを使用することもできます。
if (document.getElementById('myTextBox').value == "")
{
return confirm('Are you sure you want to exit without filling in ... ?');
}