-1

I need to check if input has only numbers and spaces(no letters or characters).

Tried using .includes


var string = 'abc'

if(string.includes(A-Za)){
console.log('bad')
} else {
console.log('good')
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
TheMisterS
  • 11
  • 1
  • do you like to check if it contains some characters or if it does not contain some characters? – Nina Scholz Jun 26 '19 at 08:13
  • [`RegExp.prototype.test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) – Yevhen Horbunkov Jun 26 '19 at 08:13
  • @NinaScholz i would like to check if it cointains any letters or characters(as if: commas,dots, clauses) – TheMisterS Jun 26 '19 at 08:15
  • please edit your question and add some more use cases. – Nina Scholz Jun 26 '19 at 08:16
  • So do you only want to accept numbers and spaces. or reject any string that contains atleast one character? If its the later, do you want to reject all characters or just the english dictionary? Should `,./;` or `Γк` pass the check? – nick zoum Jun 26 '19 at 08:21

5 Answers5

2

Using Regex.test()

var string = 'abc'

if (/[A-Za-z]/.test(string)) {
  console.log('bad')
} else {
  console.log('good')
}
User863
  • 19,346
  • 2
  • 17
  • 41
  • *I need to check if input has only numbers and spaces* . Your regex is not correct. That allow for example : `; ? / + =` – R3tep Jun 26 '19 at 08:19
  • This won't always work if `string = "?"` you will get "good", but it actually isn't – Nick Parsons Jun 26 '19 at 08:19
1

Just use a simple regex that matches numbers from start to end:

const bad = 'abc';
const good = 123;
const re = /^\d*$/;

const goodOrBad = str => re.test(str) ? "Good" : "Bad";

console.log(goodOrBad(bad));
console.log(goodOrBad(good));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

console.log(check("abc"));
console.log(check("123"));
console.log(check("123 123"));
console.log(check("123 abc"));

function check(txt) {
  return /^(\d|\s)*$/.test(txt) ? "good" : "bad";
}

Breakdown of: ^(\d|\s)*$

  • ^: Start of string
  • $: End of string
  • \d: Match a number (Can also be written as [0-9])
  • \s: Match a space or any other whitespace character (if you just want space then )
  • \d|\s: Match number or space
  • (\d|\s)*: Match number or space 0-Many times (Can also be written as (\d|\s){0,})
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • This to me looks like the correct solution, checking for both numbers and spaces, where the other answers do not – Nick Parsons Jun 26 '19 at 08:21
  • 1
    @NickParsons Not so sure anymore because OP said `i would like to check if it cointains any letters or characters(as if: commas,dots, clauses)`. – nick zoum Jun 26 '19 at 08:24
  • U either check if it has only numbers and spaces or if it has any numbers or characters. Its just reversed. – TheMisterS Jun 26 '19 at 08:35
  • @TheMisterS Should a string that contains neither (i.e. `,./,`) print `good` or `bad`? – nick zoum Jun 26 '19 at 09:12
0

Try this with this regex ^[0-9]*$

console.log( /^[0-9]*$/.test('45') ? "Good" : "Bad")

enter image description here

Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23
0

checking every character:

for (i = 0; i < string.length; i++) { 
  if (isNan(string[i]) && string[i] == ' ') {
  console.log('bad')
} else {
  console.log('good')
}
}
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23