Je veux valider cette page en utilisant Html Validation comme indiqué dans le code. Lorsque btnSubmit
est cliqué, il ira directement à SaveSubject
Il n'y a pas d'erreur lorsque la fonction btnSubmit
est cliqué. Mais quand je crée un simple bouton sans postback, il donne une erreur pour entrer dans le champ requis, etc. Je veux valider en utilisant btnSubmit
( LinkButton
), il doit donc d'abord valider puis passer à l'appel ajax. J'ai essayé event.preventDefault();
Code Jquery :
$(document).ready(function () {
$("[id*=btnSubmit]").bind("click", function () {
var subject = {};
subject.SubjectName = $("[id*=TxtSubjectName]").val();
subject.CourseCode = $("[id*=TxtCourseCode]").val();
subject.BookName = $("[id*=TxtBookName]").val();
subject.AuthorName = $("[id*=TxtAuthorName]").val();
subject.Edition = $("[id*=TxtEdition]").val();
$.ajax({
url: "manage-subject.aspx/SaveSubject",
type: "POST",
data: '{subject: ' + JSON.stringify(subject) + '}',
dataType: "json",
contentType: "application/json; charset=utf-8",
datafilter: function (data) { return data; },
success: function (response) {
if (response.d != null) {
sweetAlert("Oops...", response.d, "error");
}
if (response.d == null)
swal("Good job!", "Subject has been added successfully!", "success");
}
});
return false;
});
});
Code ASP.NET :
<head>
<body>
<form id="form1" runat="server">
<label class="control-label" for="TxtSubjectName">Subject Name
</label>
<asp:TextBox ID="TxtSubjectName" class="form-control m-wrap span12" placeholder="Enter Subject Name" pattern="^([a-zA-Z\s]{2,50})$" title="Must Contain Alphatbets Not More than 50 characters" runat="server" required="">
</asp:TextBox>
<label class="control-label" for="TxtCourseCode">
Course Code
</label>
<asp:TextBox ID="TxtCourseCode" class="form-control m-wrap span12" placeholder="Enter Course Code" pattern="^([a-zA-Z\s]{2,50})$" title="Must Contain Alphatbets Not More than 50 characters" runat="server">
</asp:TextBox>
<label class="control-label" for="TxtEdition">Book Name</label>
<asp:TextBox ID="TxtBookName" class="form-control m-wrap span12" placeholder="Enter Book Name" pattern="^([a-zA-Z\s]{2,100})$" title="Must Contain Alphatbets Not More than 100 characters" runat="server" required=""></asp:TextBox>
<label class="control-label" for="TxtAuthorName">Author Name</label>
<asp:TextBox ID="TxtAuthorName" class="form-control m-wrap span12" placeholder="Enter Author Name" pattern="^([a-zA-Z\s]{2,50})$" title="Must Contain Alphatbets Not More than 50 characters" runat="server" required=""></asp:TextBox>
<label class="control-label" for="TxtEdition">Edition</label>
<asp:TextBox ID="TxtEdition" class="form-control m-wrap span12" placeholder="Enter Book Edition" pattern="^([a-zA-Z\s]{2,15})$" title="Must Contain Alphatbets Not More than 15 characters" runat="server"></asp:TextBox>
<asp:LinkButton ID="btnSubmit" type="submit" class="btn green btn-block" runat="server">Add Subject <i aria-hidden="true" class="m-icon-swapright m-icon-white"></i></asp:LinkButton>
</form>
</body>
</head>