How to make Joi regex() validation fail if the string contains “ ” (whitespace)?
I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)
I tried the following schema, but Joi does let it go through:
const schema = {
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}
So, if I submit "JOI 777" the string is considered valid.
What am I doing wrong?
Thanks in advance,
javascript node.js joi
add a comment |
I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)
I tried the following schema, but Joi does let it go through:
const schema = {
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}
So, if I submit "JOI 777" the string is considered valid.
What am I doing wrong?
Thanks in advance,
javascript node.js joi
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace/^S+$/
, IOW: if a whitepace appears it should fail.
– Keith
Nov 20 '18 at 12:03
add a comment |
I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)
I tried the following schema, but Joi does let it go through:
const schema = {
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}
So, if I submit "JOI 777" the string is considered valid.
What am I doing wrong?
Thanks in advance,
javascript node.js joi
I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)
I tried the following schema, but Joi does let it go through:
const schema = {
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}
So, if I submit "JOI 777" the string is considered valid.
What am I doing wrong?
Thanks in advance,
javascript node.js joi
javascript node.js joi
asked Nov 20 '18 at 11:47
Nikolay ChekanNikolay Chekan
31
31
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace/^S+$/
, IOW: if a whitepace appears it should fail.
– Keith
Nov 20 '18 at 12:03
add a comment |
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace/^S+$/
, IOW: if a whitepace appears it should fail.
– Keith
Nov 20 '18 at 12:03
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace
/^S+$/
, IOW: if a whitepace appears it should fail.– Keith
Nov 20 '18 at 12:03
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace
/^S+$/
, IOW: if a whitepace appears it should fail.– Keith
Nov 20 '18 at 12:03
add a comment |
2 Answers
2
active
oldest
votes
This part of your regex -> /^.*
is saying match anything, so the rest of your regEx is pretty much short circuited.
So your RegEx is a bit simpler, /^S+$/
This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()
..
eg.
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
add a comment |
for skipping whitespace from string just use:
"hello world".replace(/s/g, "");
if you have more than One space use this :
"this string has more than one space".replace(/ /g, '');
for more detail see this link below:
Remove whitespaces inside a string in javascript
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.
– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392351%2fhow-to-make-joi-regex-validation-fail-if-the-string-contains-whitespace%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This part of your regex -> /^.*
is saying match anything, so the rest of your regEx is pretty much short circuited.
So your RegEx is a bit simpler, /^S+$/
This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()
..
eg.
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
add a comment |
This part of your regex -> /^.*
is saying match anything, so the rest of your regEx is pretty much short circuited.
So your RegEx is a bit simpler, /^S+$/
This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()
..
eg.
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
add a comment |
This part of your regex -> /^.*
is saying match anything, so the rest of your regEx is pretty much short circuited.
So your RegEx is a bit simpler, /^S+$/
This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()
..
eg.
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
This part of your regex -> /^.*
is saying match anything, so the rest of your regEx is pretty much short circuited.
So your RegEx is a bit simpler, /^S+$/
This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()
..
eg.
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];
tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});
answered Nov 20 '18 at 12:13
KeithKeith
8,8301820
8,8301820
add a comment |
add a comment |
for skipping whitespace from string just use:
"hello world".replace(/s/g, "");
if you have more than One space use this :
"this string has more than one space".replace(/ /g, '');
for more detail see this link below:
Remove whitespaces inside a string in javascript
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.
– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
add a comment |
for skipping whitespace from string just use:
"hello world".replace(/s/g, "");
if you have more than One space use this :
"this string has more than one space".replace(/ /g, '');
for more detail see this link below:
Remove whitespaces inside a string in javascript
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.
– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
add a comment |
for skipping whitespace from string just use:
"hello world".replace(/s/g, "");
if you have more than One space use this :
"this string has more than one space".replace(/ /g, '');
for more detail see this link below:
Remove whitespaces inside a string in javascript
for skipping whitespace from string just use:
"hello world".replace(/s/g, "");
if you have more than One space use this :
"this string has more than one space".replace(/ /g, '');
for more detail see this link below:
Remove whitespaces inside a string in javascript
answered Nov 20 '18 at 12:11
babak abadkheirbabak abadkheir
479311
479311
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.
– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
add a comment |
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.
– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
1
1
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.– Keith
Nov 20 '18 at 12:21
if you have more than One space use this
, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.– Keith
Nov 20 '18 at 12:21
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
tnx for the tips I realized now
– babak abadkheir
Nov 20 '18 at 12:25
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.
– unflores
Nov 20 '18 at 13:30
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!
– Nikolay Chekan
Nov 21 '18 at 9:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392351%2fhow-to-make-joi-regex-validation-fail-if-the-string-contains-whitespace%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace
/^S+$/
, IOW: if a whitepace appears it should fail.– Keith
Nov 20 '18 at 12:03