in this function i want to add pan gstn mobile email validation please suggest
function ValidateForm() {
if ($("#txtname1").val() == '') {
//alert('Please Enter Name.');
return false;
}
}

Sachin SinghRank #66 · 26.2K reputationPosted Jan 10, 2024, 12:29 AM EST
you can use this
function ValidateForm() { var name = $("#txtname1").val(); var gstn = $("#txtGSTN").val(); var pan = $("#txtPAN").val(); var mobile = $("#txtMobile").val(); var email = $("#txtEmail").val(); // Validation for Name if (name.trim() === '') { alert('Please Enter Name.'); return false; } // Validation for GSTN var gstnRegex = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{2}[Z]{1}[0-9A-Z]{1}$/; if (!gstnRegex.test(gstn)) { alert('Please Enter a Valid GSTN.'); return false; } // Validation for PAN var panRegex = /[A-Z]{5}[0-9]{4}[A-Z]{1}$/; if (!panRegex.test(pan)) { alert('Please Enter a Valid PAN.'); return false; } // Validation for Mobile var mobileRegex = /^[0-9]{10}$/; if (!mobileRegex.test(mobile)) { alert('Please Enter a Valid Mobile Number.'); return false; } // Validation for Email var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { alert('Please Enter a Valid Email Address.'); return false; } return true; // Form is valid }Ramakrishnan RRank #998 · 2K reputationPosted Jan 10, 2024, 12:28 AM EST
To add PAN, GSTN, mobile, and email validation to your JavaScript function, you can modify it as follows:
This code includes validations for PAN, GSTN, mobile number, and email address. Make sure to replace
#txtpan,#txtgstn,#txtmobile, and#txtemailwith the appropriate IDs for your input fields.You can also customize the regular expressions (
panRegex,gstnRegex,mobileRegex,emailRegex) based on the specific validation requirements you have.Amit MohantyRank #112 · 20K reputationPosted Jan 10, 2024, 1:01 AM EST
function ValidateForm() { const validations = [ { fieldId: 'txtname1', validation: 'notEmpty', message: 'Please Enter Name.' }, { fieldId: 'txtpan', validation: 'pan', message: 'Please Enter a valid PAN.' }, { fieldId: 'txtgstn', validation: 'gstn', message: 'Please Enter a valid GSTN.' }, { fieldId: 'txtmobile', validation: 'mobile', message: 'Please Enter a valid Mobile Number.' }, { fieldId: 'txtemail', validation: 'email', message: 'Please Enter a valid Email.' } ]; for (let i = 0; i < validations.length; i++) { const fieldId = validations[i].fieldId; const validation = validations[i].validation; const message = validations[i].message; const value = $("#" + fieldId).val(); if (validation === 'notEmpty' && value.trim() === '') { alert(message); return false; } else if (validation === 'pan' && !validatePAN(value)) { alert(message); return false; } else if (validation === 'gstn' && !validateGSTN(value)) { alert(message); return false; } else if (validation === 'mobile' && !validateMobile(value)) { alert(message); return false; } else if (validation === 'email' && !validateEmail(value)) { alert(message); return false; } } return true; } // PAN Validation function validatePAN(panValue) { const panRegex = /[A-Z]{5}[0-9]{4}[A-Z]{1}/; return panRegex.test(panValue.toUpperCase()); } // GSTN Validation function validateGSTN(gstnValue) { const gstnRegex = /^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[1-9A-Z]{1}Z\d{1}$/; return gstnRegex.test(gstnValue.toUpperCase()); } // Mobile Validation function validateMobile(mobileValue) { const mobileRegex = /^[6-9]\d{9}$/; return mobileRegex.test(mobileValue); } // Email Validation function validateEmail(emailValue) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(emailValue); }Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 10, 2024, 1:10 AM EST
Sure, I can help you add validation for PAN, GSTN, mobile, and email in your JavaScript function. Here’s how you can do it:
function ValidateForm() { var panPattern = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/; var gstnPattern = /^([0-9]){2}([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}([1-9A-Z]){1}Z{1}[0-9a-zA-Z]{1}?$/; var mobilePattern = /^(\+\d{1,3}[- ]?)?\d{10}$/; var emailPattern = /^\S+@\S+\.\S+$/; var pan = $("#txtPan").val(); var gstn = $("#txtGstn").val(); var mobile = $("#txtMobile").val(); var email = $("#txtEmail").val(); if ($("#txtname1").val() == '') { //alert('Please Enter Name.'); return false; } if (!panPattern.test(pan)) { //alert('Please Enter valid PAN.'); return false; } if (!gstnPattern.test(gstn)) { //alert('Please Enter valid GSTN.'); return false; } if (!mobilePattern.test(mobile)) { //alert('Please Enter valid Mobile.'); return false; } if (!emailPattern.test(email)) { //alert('Please Enter valid Email.'); return false; } return true; }In this code, I’ve added regular expressions to validate PAN, GSTN, mobile, and email. You need to replace
$("#txtPan").val(),$("#txtGstn").val(),$("#txtMobile").val(), and$("#txtEmail").val()with the actual IDs of your input fields. If the validation fails, the function will returnfalse. You can uncomment the alert statements to show an alert to the user when the validation fails. Please note that this is a basic validation and might need to be adjusted based on your specific requirements. For example, the mobile number validation is very basic and checks only for 10 digits. It does not check for country codes or specific number patterns within different countries. Similarly, the email validation is also basic and checks for the presence of ‘@’ and ‘.’ symbols in the correct order. It does not check for valid top-level domains or the exact number of characters in each part of the email. You might want to adjust these regular expressions based on your requirements. Also, please be aware that client-side validation can be bypassed, so make sure to validate the data on the server side as well.Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 10, 2024, 4:15 AM EST
If you want to display an alert message when the name field is empty, you can uncomment the line
//alert('Please Enter Name.');by removing the double slashes at the beginning of the line. This will enable the alert message to be displayed.Here's the updated code:
function ValidateForm() { if ($("#txtname1").val() == '') { alert('Please Enter Name.'); return false; } }To prevent the form from being submitted when the name field is empty, the current code is sufficient. However, if you want to add additional validation for PAN, GSTN, mobile, and email fields, you can modify the
ValidateForm()function accordingly.Here's an example of how you can add validation for PAN, GSTN, mobile, and email fields:
function ValidateForm() { var name = $("#txtname1").val(); var pan = $("#txtpan").val(); var gstn = $("#txtgstn").val(); var mobile = $("#txtmobile").val(); var email = $("#txtemail").val(); if (name == '') { alert('Please Enter Name.'); return false; } if (pan == '') { alert('Please Enter PAN.'); return false; } // Add validation for GSTN, mobile, and email fields here // Example GSTN validation if (gstn == '') { alert('Please Enter GSTN.'); return false; } // Example mobile validation if (mobile == '') { alert('Please Enter Mobile.'); return false; } // Example email validation if (email == '') { alert('Please Enter Email.'); return false; } }In the above example, I have added variables for PAN, GSTN, mobile, and email fields and performed validation checks similar to the name field. You can customize the validation logic for each field according to your requirements.
Remember to update the HTML code to include the appropriate input fields with the corresponding ids (
txtpan,txtgstn,txtmobile,txtemail) for the respective fields.