-1

function nameValidation(){
  var x = eval(prompt("please enter a value"));
  var i = 0;
  while(i < Infinity){
    if(x !== "abcdefghi"){
      alert("please enter a valid name")
      x = eval(prompt("please enter a value"));
    }
    else if (x === "abcdefghi"){
      break
    }
    i++
 }
}
nameValidation()

I am trying to validate a name using eval() and prompt(). When I write any string I get an error:

VM146:1 Uncaught Reference Error: klklk is not defined at eval (eval at nameValidation (Java-Script:7), :1:1)at nameValidation (:7:11)at :15:1.

I tried to use other conversion methods but I got an infinite loop without stop, although I am using break in my code.

function nameValidation(){
  var x = eval(prompt("please enter a value"));
  var i = 0;
  while(i < Infinity){
    if(x !== "abcdefghi"){
      alert("please enter a valid name")
      x = eval(prompt("please enter a value"));
    }
    else if (x === "abcdefghi"){
      break
    }
    i++
 }
}

I expected the program to work without errors, on the other hand, I expect to have the alert: please enter a valid name if the name is not a string and if the name is a string the programme breaks out of the loop.

In that code the eval() evaluates the x, if it is not a string the program will alert "please enter a valid name". That's because I use identically, not equal operator ("!==") to check the type and if x identically equal to string the program breaks out of the loop. My concern is why I receive the error above in my question.

ProgrammerPer
  • 1,111
  • 1
  • 10
  • 24
  • 1
    What purpose does `eval` serve in your code? – Salman A Jan 01 '19 at 06:01
  • A good start point for `eval`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval – Sid Jan 01 '19 at 06:02
  • hi Mr Salman i would like to clarify that the eval () in code will evaluate the string if it is string the code will break if not the loop will still work – Mina Shaker Jan 01 '19 at 06:07
  • The return value from `prompt()` is *always* a string. You're getting that error because the characters you type in response to the prompt do not correspond to any declared variable. – Pointy Jan 01 '19 at 06:31
  • @pointy so what is the solution from your point of view – Mina Shaker Jan 01 '19 at 06:34
  • @MinaShaker Solution is provided here: https://stackoverflow.com/questions/53993317/what-are-the-reason-that-i-got-error-when-i-use-eval-with-prompt/53993551#53993551 – Sami Ahmed Siddiqui Jan 01 '19 at 06:48
  • `eval` is not for validation. For the first part you don't want to use `eval` but [(Built-in) way in JavaScript to check if a string is a valid number](https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) – t.niese Jan 01 '19 at 08:03
  • But the second part:`I expect to have alert please enter a valid name if the name is not a string and if the name is a string the programe breaks out the loop` is not clear `prompt` returns always a strings, entering `1234` in the prompt returns the string with the content `1234` and not a number. I know that `1234` is not a name, but let's change it to `a123` or `abcd` that's not a name or a number either but also not a name. So I don't get the purpose of that test. – t.niese Jan 01 '19 at 08:03

3 Answers3

-1

The eval method with prompt does not return anything, and hence, the value of your variable x is undefined. Instead of eval & prompt, you can only use prompt.

function nameValidation(){
  var x = prompt("please enter a value");  //changed this
  var i = 0;
  while(i < Infinity){
    if(x !== "abcdefghi"){
      alert("please enter a valid name")
      x = prompt("please enter a value"); // changed this
    }
    else if (x === "abcdefghi"){
      break
    }
    i++
 }
}

The larger question is that why did you need eval in the first place. Also, your check for i<Infinity and i++ in the loop does not make much sense, as i will never exceed Infinity

Sid
  • 4,772
  • 14
  • 57
  • 107
  • 2
    Not sure about this: *The `eval()` method does not return anything* – Prashant Pimpale Jan 01 '19 at 06:09
  • 2
    because this-> `var a = eval("10 * 20");console.log(a);` prints: `200` – Prashant Pimpale Jan 01 '19 at 06:11
  • in that code the eval() evaluates the x if it is not a string the program will alert "please enter a valid name" that because i use identically not equal operator ("!==") to check the type and if x identically equal to string the program breaks out the loop. my concern why i receive the error above in my question. – Mina Shaker Jan 01 '19 at 06:17
-1

As per documentation the eval() function expects a string which you are missing in your code:

eval(string)

where

Parameters

string

A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Return value

The completion value of evaluating the given code. If the completion value is empty, undefined is returned.

Reference Link

You just need to make a simple change in eval as shown below:

/**
 * Just correcting the error in the mentioned code of the question
 */ 
function nameValidation(){
  var x = eval('prompt("please enter a value")');
  var i = 0;
  while(i < Infinity){
    if(x !== "abcdefghi"){
      alert("please enter a valid name")
      x = eval('prompt("please enter a value")');
    }
    else if (x === "abcdefghi"){
      break;
    }
    i++;
 }
}

/**
 * Fixing Eval() issue as mentioned in code as well as checking that the string should not be empty.
 */ 
function nameValidation2(){
    var x = eval('prompt("please enter a value")');
    var i = 0;
    while(i < Infinity){
      if(x == ""){
          alert("please enter a valid name")
          x = eval('prompt("please enter a value")');
      }
      else {
          break;
      }
      i++;
   }
}
nameValidation2();
Sami Ahmed Siddiqui
  • 2,150
  • 1
  • 15
  • 28
  • When I tried the code i didn’t break out of the loop and it became infinite loop the only way to get out is to write the same string “abcdefghi” what I need to do to check if x is a string then the loop breaks out if not variable x loops again. – Mina Shaker Jan 01 '19 at 07:09
  • @MinaShaker I have only provided the solution you have mentioned in your code. Remaining code is same as your provided one. I don't know why you wrote those conditions and what you want achieve. It all depends on you. – Sami Ahmed Siddiqui Jan 01 '19 at 07:41
  • @MinaShaker Snippet updated. Added another function with the name of `nameValidation2`. It check the prompt should not be empty. – Sami Ahmed Siddiqui Jan 01 '19 at 07:49
  • Those evals in your code are completely useless `x = eval('prompt("please enter a value")')` and `x = prompt("please enter a value")` will have the same result. – t.niese Jan 01 '19 at 08:07
  • @t.niese I am agree that the `eval` is useless here but i have not added it by myself. I am only answering the question what have been asked. – Sami Ahmed Siddiqui Jan 01 '19 at 10:19
  • You changed the code form `eval(prompt("please enter a value"));` to `eval('prompt("please enter a value")');` adding those `'` around the `prompt("please enter a value")`. But if you suggest that this solves the problem, then you should remove that `eval` completely, because `x = eval('prompt("please enter a value")')` and `x = prompt("please enter a value")` are equal with respect to what will happen. Otherwise your answer falsely suggests that `eval` has any effect in your answer. Besides that `prompt` returns in fact a string. (or `null` if canceled) – t.niese Jan 01 '19 at 10:23
-2

No need to use eval(), use the Regular_expression to check whether an entered value is not integer only. As per suggestion from t-niese there are the names who contains special characters like ',é, ö still many more which we don't even know about, ex:

Prashant D'Souza
Mary Kate 
André 
Sören
Jørgen
Prashant0123 -- in case of username 

You can also read about Special characters in the name here and Regular_Expressions Guide by Mozilla

function nameValidation() {
 var x = prompt("please enter a value");
 var i = 0;
 var regex = /^[0-9]+$/;

 while (i < Infinity) {
  if (x === "abcdefghi") {
   break;
  } else if (!x.match(regex)) {
   break;
  } else {
   alert("please enter a valid name")
   x = prompt("please enter a value")
  }
  i++;
 }
}
nameValidation();
Prashant Pimpale
  • 9,685
  • 7
  • 35
  • 75
  • 1
    I didn’t studied regex type yet but I think this solve the whole problem thanks – Mina Shaker Jan 01 '19 at 07:58
  • Glad it helps..! – Prashant Pimpale Jan 01 '19 at 07:59
  • 1
    `[a-zA-Z]` is really limited regarding names `Mary Kate`, `André`, `Sören`, `Jørgen`, ... would all not be valid name. (Ok I don't know if name is really about family names, but even for variable names `abc23` would be valid` – t.niese Jan 01 '19 at 08:19
  • @t.niese updated for special character case and to allow space for full name! Thanks for your valuable suggestions, Just had a deep overview of Regex! But still, need to improve, names like `D' Souza` is still a valid name – Prashant Pimpale Jan 01 '19 at 08:47
  • 2
    I actually don't see so much sens in doing such a validation anyway. If you want to accept any possible name then you a condition that check that the name is not empty (or probably not numerical) should be sufficient. There are so many variation of names that you cannot do a reliable test if it is a name. – t.niese Jan 01 '19 at 09:05
  • @t.niese yes as said `prashant003` is also a valid name, I have also had a look at [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) – Prashant Pimpale Jan 01 '19 at 09:12
  • @t.niese Thanks a lot! – Prashant Pimpale Jan 01 '19 at 09:23