repocrypts / Validator

Client-side javascript validator library ports from Laravel 5.2
MIT License
46 stars 22 forks source link

regex bug #28

Closed BackdoorTech closed 2 years ago

BackdoorTech commented 3 years ago

when regular expression contains comma ',' example:

/[0-9]{1,8}/

Not working properly

i solved it this way


const data = {
  name: 'John Doe',
  company: 'Example Co.',
  birthday: '3123'
};

const rules = {
  name: 'required',
  birthday: [`EMISREX: [0-9]{1,8}`],
  company: ['required', 'string']
};

const v = Valid.make(data, rules);
function EMISREX (name, value, params) {

  if (typeof(params) === 'object') {
    var params = params.join() // convert array into string
  }
  params = params.replace(/\s*/,''); // remove  spaces from the beginning of the text
  params = new RegExp(params);
  return value.match(params) !== null
}

v.extend('EMISREX', EMISREX, ':attr is not a valid mongo id');

if (v.fails()) {
  const errors = v.getErrors();
  console.log(errors);
}

I just test the code in working

This way won't work

const data = {
  name: 'John Doe',
  company: 'Example Co.',
  birthday: '3123'
};

const rules = {
  name: 'required',
  birthday: [`regex: /[0-9]{1,8}/`],
  company: ['required', 'string']
};

const v = Valid.make(data, rules);

if (v.fails()) {
  const errors = v.getErrors();
  console.log(errors);
}

I love your library!!!!

jfstn commented 3 years ago

Hello 👋

I'll be checking this issue soon. Thanks for your patience.