I have two forms in a user control, posting an email with jQuery and Ajax. Unfortunately, our cms using .NET Web Forms and master pages, which makes name attribute changing. This makes it difficult to use jquery validate, since it is based on the attribute name. Do you have any suggestions how I can solve this ?
Code
$(function () {
$("#tbSendComment").click(function (e) {
$('#formComment').validate({
rules: {
ctl00$mainPlaceHolder$ctl02$tbCommentSubject: "required",
ctl00$mainPlaceHolder$ctl02$tbCommentMessage: "required",
ctl00$mainPlaceHolder$ctl02$tbCommentName: "required",
ctl00$mainPlaceHolder$ctl02$tbCommentEmail: {
required: true,
email: true
}
},
submitHandler: function (form) {
var obj = {};
var Uri = '<%: Uri %>';
var Title = '<%: Title %>';
obj.subject = $("#tbCommentSubject").val();
obj.message = $("#tbCommentMessage").val();
obj.name = $("#tbCommentName").val();
obj.fromEmail = $("#tbCommentEmail").val();
obj.cc = $("#cbSendMeCoppyComment").prop('checked');
obj.title = Title;
obj.uri = Uri;
var jsonData = JSON.stringify(obj);
$.ajax({
type: "POST",
url: "/leaveacomment",
data: jsonData,
success: function successFunc(data) {
etc
etc
You can solve this problem easily by Using Web Form's special solution:
Instead of writing the whole name hard-coded, use the dynamic control.UniqueID
control.UniqueID
is what .NET use for your element name
attribute on the Client side
Unfortunately, our cms using .NET Web Forms and master pages, which makes name attribute changing.
You will never have to worry about your elements name changes because it will dynamically change with your elements name changes
In the Client side use it like:
var name = "<%= txtTest.UniqueID %>";