Enterprise Framework

Software Solutions in the Enterprise

How To: Unobtrusive Javascript - Manually Add Required Rule and Message.

How To: Unobtrusive Javascript - Manually Add/Remove Required Rule and Message.  

<script type="text/javascript">
$(function () {

var addRequired = function () {
// Manually add required fields to certain fields
$('#scheduledLockDate').rules('add', {
required: true,
messages: {
required: "The Scheduled Lock Date is required"
}
})
;
$('#scheduledDisposeDate').rules('add', {
required: true,
messages: {
required: "The Scheduled Dispose Date is required"
}
})
;
$('#issueDate').rules('add', {
required: true,
messages: {
required: "The Issue Date is required"
}
})
;
}

// This will remove the manually required fields
var removeRequired = function () {
// Manually remove required fields to certain controls
$('#scheduledLockDate').rules('remove', 'required');
$('#scheduledDisposeDate').rules('remove', 'required');
$('#issueDate').rules('remove', 'required');
}

// Handle the form submit method
$('#workspaceForm').submit(function (e) {
e.preventDefault();
var form = this;

// Trigger Form Validation
var isValid = $('#workspaceForm').valid();

if (!isValid) {
// Exit out
return false;
}

form.submit();
});

//Manually add the required fields
addRequired();

//Manually remvoe the required fields
removeRequired();
});
</script>
Reference:  https://jqueryvalidation.org/rules/

Comments are closed