次のように、Web フォームからユーザー入力を取得しています。
Dim t_ResolvedID As TextBox = DirectCast(gvrow.FindControl("editResolved"), TextBox)
Dim t_CommentsID As TextBox = DirectCast(gvrow.FindControl("editComments"), TextBox)
許容される入力を次のように制限したいと思います。
- t_ResolvedID は正の整数のみにする必要があります (英字は使用できません)
- t_CommentsID は 4000 文字を超えてはなりません。さらに、t_CommentsID.Text に一重引用符が含まれている場合は、2 つの一重引用符に置き換えます。
現在、このエラー処理を次のように実行しています。
If IsNumeric(t_ResolvedID.Text) Then
resolved = Integer.Parse(t_ResolvedID.Text)
Else
ShowMessage("Error! Invalid character in 'Resolved' field.")
errorCount += 1
End If
If Integer.Parse(t_ResolvedID.Text) < 0 Then
ShowMessage("Error! 'Resolved' field cannot be negative!")
errorCount += 1
End If
If t_CommentsID.Text.Length > 4000 Then
errorCount += 1
ShowMessage("Error! The 'Comments' field cannot exceed 4000 characters!")
End If
'Transform single quote into two single quotes to avoid SQL errors
If t_CommentsID.Text.Contains("'") Then
comments = t_CommentsID.Text.Replace("'", "''")
End If
If t_CommentsID.Text.Length < 4000 And Not t_CommentsID.Text.Contains("'") Then
comments = t_CommentsID.Text
End If
もっといい方法があるような気もしますが。悪いデータで最終的な更新 SQL クエリを実行したくないので、現在、エラー カウントのみを保持しています。そのため、クエリを実行する前に errorCount が 0 に等しいかどうかを確認します。これをより効率的にするにはどうすればよいですか?
ShowMessage() 関数に AJAX を使用しているので、可能であればユーザーにエラーを通知する機能を維持したいと考えています。
ありがとうございました!
編集:次のようにコードを変更することになりました:
If Not IsNumeric(t_ResolvedID.Text) Then
errors += "Error! Invalid character in 'Resolved' field<br/>"
Else
resolved = Integer.Parse(t_ResolvedID.Text)
If resolved < 0 Then
errors += "Error! 'Resolved' field cannot be negative!<br/>"
Else
resolved = t_ResolvedID.Text
End If
End If
If t_CommentsID.Text.Length > 4000 Then
'errorCount += 1
errors += "Error! 'Comments' field cannot exceed 4000 characters!<br/>"
End If
'Transform single quote into two single quotes to avoid SQL errors
If t_CommentsID.Text.Contains("'") Then
comments = t_CommentsID.Text.Replace("'", "''")
End If
If t_CommentsID.Text.Length < 4000 And Not t_CommentsID.Text.Contains("'") Then
comments = t_CommentsID.Text
End If