{"id":9192,"date":"2024-09-23T07:06:00","date_gmt":"2024-09-23T07:06:00","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9192"},"modified":"2024-09-23T07:39:19","modified_gmt":"2024-09-23T07:39:19","slug":"validate-vtiger-crm-6-2-custom-fields-with-jquery-easily","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/validate-vtiger-crm-6-2-custom-fields-with-jquery-easily\/","title":{"rendered":"Validate Vtiger CRM 6.2 Custom Fields with jQuery Easily"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,772<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p class=\"wp-block-paragraph\">To validate fields in Vtiger CRM 6.2 for a custom module using jQuery, you can follow these steps. This guide assumes that you&#8217;re customizing the module&#8217;s form and want to ensure that specific fields are validated before submission.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step-by-Step Guide:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Identify the Form:<\/strong><br>Vtiger&#8217;s forms for custom modules typically have an ID like <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">EditView<\/mark><\/code>. You need to hook into this form using jQuery.<\/li>\n\n\n\n<li><strong>Add jQuery Validation:<\/strong><br>Use jQuery to validate the form fields on form submission or on a specific event (e.g., <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">blur<\/mark><\/code> event when the user leaves the field).<\/li>\n\n\n\n<li><strong>Custom Validation Logic:<\/strong><br>Create custom validation logic depending on the fields you want to validate (e.g., checking if a field is empty, or if it has a valid email format).<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Sample jQuery Code:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example that shows how to validate a custom field (e.g., &#8220;phone number&#8221; and &#8220;email&#8221;) in a Vtiger form using jQuery:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\">&lt;\/script>\n\n&lt;script>\n$(document).ready(function() {\n    \/\/ On form submission\n    $('#EditView').submit(function(event) {\n        var isValid = true;\n\n        \/\/ Validate phone number field (assuming field name is 'phone')\n        var phoneNumber = $('input&#91;name=\"phone\"]').val();\n        if (phoneNumber == '') {\n            isValid = false;\n            alert('Phone number cannot be empty.');\n        } else if (!\/^\\d{10}$\/.test(phoneNumber)) {\n            isValid = false;\n            alert('Phone number must be 10 digits.');\n        }\n\n        \/\/ Validate email field (assuming field name is 'email')\n        var email = $('input&#91;name=\"email\"]').val();\n        if (email == '') {\n            isValid = false;\n            alert('Email cannot be empty.');\n        } else if (!\/^&#91;a-zA-Z0-9._-]+@&#91;a-zA-Z0-9.-]+\\.&#91;a-zA-Z]{2,6}$\/.test(email)) {\n            isValid = false;\n            alert('Invalid email format.');\n        }\n\n        \/\/ If the form is not valid, prevent submission\n        if (!isValid) {\n            event.preventDefault(); \/\/ Stop form submission\n        }\n    });\n});\n&lt;\/script><\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Form Selection:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">$('#EditView')<\/mark><\/code>: Selects the form by its ID (commonly <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">EditView<\/mark><\/code> in Vtiger custom modules).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">     2. <strong>Field Selection:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">$('[name=\"phone\"]')<\/mark><\/code>: Selects the phone field by its name attribute. Adjust this to match the name of the field in your custom module.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    3. <strong>Validation Logic:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The phone number is checked to ensure it is not empty and contains exactly 10 digits.<\/li>\n\n\n\n<li>The email is validated against a regular expression to ensure it&#8217;s in the correct format.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">    4. <strong>Prevent Submission:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">event.preventDefault()<\/mark><\/code> is called if validation fails, stopping the form from being submitted.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How to Use:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Place the script in your custom module\u2019s <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">EditView.tpl<\/mark><\/code> or equivalent template file.<\/li>\n\n\n\n<li>Adjust the field names (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">phone<\/mark><\/code>, <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">email<\/mark><\/code>) to match the actual names used in your custom module.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Validations:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can add more validations as needed. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Validate that a date field contains a valid date.<\/li>\n\n\n\n<li>Ensure that a number field contains only numeric values.<\/li>\n\n\n\n<li>Check for specific formats (like postal codes).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Tips:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If your form already includes a submit button with JavaScript, you may want to integrate your validation logic into that instead of creating a new <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">submit<\/mark><\/code> handler.<\/li>\n\n\n\n<li>Use <strong>CSS<\/strong> to highlight the fields with errors by adding error classes using jQuery.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This approach ensures real-time field validation in your Vtiger 6.2 custom module using jQuery. Let me know if you need further assistance!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To easily validate all the necessary fields in Vtiger  6.2 for any specific custom module with the help of performing jQuery, you can obey all above-mentioned steps. This whole guide accepts that you\u2019re tailoring the module\u2019s form with the <a href=\"https:\/\/www.infinitivehost.com\/managed-vtiger-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>best Vtiger hosting solutions<\/strong><\/mark><\/a> and need to make sure that all crucial fields are validated before submission.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,772 Views To validate fields in Vtiger CRM 6.2 for a custom module using jQuery, you can follow these steps. This guide assumes that you&#8217;re customizing the module&#8217;s form and want to ensure that specific fields are validated before submission. Step-by-Step Guide: Sample jQuery Code: Here is an example that shows how to validate a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[204],"tags":[],"class_list":["post-9192","post","type-post","status-publish","format-standard","hentry","category-vtiger-solutions"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9192"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9192\/revisions"}],"predecessor-version":[{"id":9197,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9192\/revisions\/9197"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}