Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

javascript - Vuelidate match / sameAs but ignore case sensitivity?

I'm using vuelidate on a form and have two inputs "First Name" and "Last Name" then in another section of the form is another input that combines the two "first name last name" and the must match which is working fine and validates.

What I would like is for the case sensitivity to not matter. so as long as the strings are equal the casing does not matter and still will match and validate. Is there a way to incorporate a regular expression? or possible convert all to lowercase?

  firstName: {
          required,
        },
  lastName: {
          required,
        },
  signature: {
        required,
        validSignature: (value, vm) => {
          const trimmedInput = value.trim().replace(/s+/g, ' ')
          const first = vm.firstName
          const last = vm.lastName

          
          if (trimmedInput.toLowerCase() === 'signature on file') {
            return true
          }
         
          if (first == null || last == null) return false

          const expectedValue = first.concat(' ', last)

          return trimmedInput === expectedValue
        },
      },
question from:https://stackoverflow.com/questions/65924058/vuelidate-match-sameas-but-ignore-case-sensitivity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can achieve this pretty easy with Regex or just simply converting your string to lower/upper case before the comparison.

Example:

const upper = 'JOHN DOE';
const lower = 'john doe';

// Simple .toLowerCare() and compare
console.log( upper.toLowerCase() === lower.toLowerCase() );

// Reg Ex w/ Ignore Case
const regex = new RegExp(lower, 'i');
console.log( regex.test(upper) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...