# Rule not accepting regex

i am applying this regex` [a-zA-Z]+(( [a-zA-Z]+)+)?` to a field but it is not validating properly, this regex results will be as follow;
John - true
John Doe - true
john Doe - true (spaces before name)
john doe -true (spaces after name)
john doe -true (spaces before and after names)

john doe -false multiple spaces between name

this is not working on multiple spaces between words.

Why not something like this for validation: https://regex101.com/r/Cd8PSD/1/tests

Or can you be more precise what you want to achieve

2 Likes

Hello @McSneaky
suggested regex is accepting more than one spaces between words, I need regex which accepts only one space between two words.
check this - https://regex101.com/r/pN8xL5/311, this fine, but adonis is not properly working on this, it also accepting multiple spaces between words in adonis

What the regex is supposed to achieve ?

only words with single space between

``````Aceeptable: John Doe - single space
Unacceptable: John  Doe - double space
``````

You can do that this way:

``````let regex = /^\w*\s\w*\$/
console.log(regex.test('AdonisJs is cool')) // false because we have more than one space
``````

actually space between the words, AdonisJs is Cool should be true as it is taking one space between AdonisJs and is and also taking one space between js and cool

1 Like

Ok, now I understand what you mean, sorry I will try to solve this in a moment

I don’t know wht, but above regex is not working as you mentioned. It is even not validating true string. What I need is below with true false matrix
John - True
John Doe - True
JohnDoe - True
John Doe -True
Jenny John Doe - True
John Doe - false (multiple space between words)
John3 Doe - false
John Doe3 - false

1 Like

I think this achieves the goal:

``````
let reg = /^([a-z]+\s)*[a-z]+\$/i
console.log(reg.test('John')) // true
console.log(reg.test('Jo2hn')) // false, 1 digit in the middle
console.log(reg.test('John Doe')) // true
console.log(reg.test('Jenny John   Doe')) // false, more than one space
console.log(reg.test('John Doe3')) // false,, 1 digit
``````

The trick is that we look for the exact pattern where an optional group consists of a word and one space can be repeated zero or more times, followed by a word.

1 Like

this may work in plain javascript, but unfortunately not working in my adonis project.

Well, there is no reason it will not work in Node.js framework Just read your code again and pay attention to what you are doing Good luck

@begueradj this the way I am trying;

``````async test({request, response, session}) {
let data =  request.only(['first_name', 'last_name'])
const rules = {
first_name: 'required|regex:/^([a-z]+\s)*[a-z]+\$/i',
last_name: 'required'
}
const validation = await validate(data, rules)
if (validation.fails()) {
session
.withErrors(validation.messages())
session.flash({ notification: {message: 'Validation failed....................!', type: 'danger'}})
return response.redirect('back')
}
return response.redirect('back')
}
``````

@trighati

For regex it’s recommended to use `rule()` https://indicative-v5.adonisjs.com/docs/regex

``````'use strict'

const { validate, rule } = use('Validator')

class TestController {
async test({ request, response, session }) {
let data = request.only(['first_name', 'last_name'])
const rules = {
first_name: [
rule('required'),
rule('regex', /^([a-z]+\s)*[a-z]+\$/i)
],
last_name: 'required'
}
const validation = await validate(data, rules)
if (validation.fails()) {
session
.withErrors(validation.messages())
session.flash({ notification: { message: 'Validation failed....................!', type: 'danger' } })
return response.redirect('back')
}
session.flash({ notification: { message: 'Validation passed', type: 'success' } })
return response.redirect('back')
}
}

module.exports = TestController
``````
1 Like