Phone Number and Name Validation using Client Script

 It performs field validation for specific fields and displays alerts if the entered data does not meet the specified criteria.

/**

 * @NApiVersion 2.1

 * @NScriptType ClientScript

 */

define([‘N/record’, ‘N/log’, ‘N/format’], function (record, log, format) {

  function validateField(context) {

    let currentRecord = context.currentRecord;

    let fieldId = context.fieldId;

    if (fieldId === ‘custrecord_jj_phone_no_otp_5805’) {

      const phone = currentRecord.getValue({ fieldId: ‘custrecord_jj_phone_no_otp_5805’ });

      const phoneRegex = /^((d{3}) d{3}-d{4}|d{11}|+d{2} d{10})$/; 

      // phone number in (000) (000)-(0000) or 09999999999 or +910000000000 these formats will be accepted

      if (!phoneRegex.test(phone)){

        alert(‘Phone number is not valid’);

        currentRecord.setValue({

          fieldId: “custrecord_jj_phone_no_otp_5805”,

          value: null,

          ignoreFieldChange: true

        })

      }

    } else if (fieldId === ‘custrecord_jj_last_date_otp_5805’) {

      const date = currentRecord.getValue({ fieldId: ‘custrecord_jj_last_date_otp_5805’ });

      let today = new Date();

      let selectedDate = new Date(date);

      if (selectedDate > today) {

        alert(‘Last Donation Date cannot be a future date.’);

        currentRecord.setValue({

          fieldId: ‘custrecord_jj_last_date_otp_5805’,

          value: today

        });

      }

    } else if (fieldId === ‘custrecord_jj_fname_otp_5805’) {

      const nameRegex = /^[a-zA-Z]+(?:[ -][a-zA-Z]+)*$/; //only text is accepted

      let name = currentRecord.getValue({ fieldId: ‘custrecord_jj_fname_otp_5805’ })

      if (!nameRegex.test(name));{

        alert(‘First Name is not valid’);

        currentRecord.setValue({

          fieldId: ‘custrecord_jj_fname_otp_5805’,

          value: null,

          ignoreFieldChange: true

        });

      }

    }

    return true;

  }

  return {

    validateField: validateField

  };

});

Leave a comment

Your email address will not be published. Required fields are marked *