-1

I am trying this code, but I don't know why it isn't working:

<script> 
    var inputtxt = '515';
    var letters = /^[0-9]+$/;  


    if(inputtxt.value.match(letters)) {
        document.write('Your registration number have accepted : you can try another');  
        return true;  
    } else {  
        document.write('Please input alphanumeric characters only');  
        return false;  
    }  
</script>  

I want to input a value (inputtxt) and then find if it contains only digits.

What am I doing wrong?

tiagodws
  • 1,345
  • 13
  • 20

2 Answers2

0

I don't know why the existing answers are getting down voted, but they are correct. If you were to use console.log(inputtxt.value) it would print undefined. You need to simply remove that from your if-else:

if(inputtxt.match(letters))
    {
        document.write('Your registration number have accepted : you can try another');
    }
    else
    {
        document.write('Please input alphanumeric characters only');
    }
demogorgon
  • 474
  • 4
  • 20
-1

Remove .value to inputtxt and return true & false from javascirpt

<script>

    var inputtxt = '515';
    var letters = /^[0-9]+$/;


    if(inputtxt.match(letters))
    {
        document.write('Your registration number have accepted : you can try another');
    }
    else
    {
        document.write('Please input alphanumeric characters only');
    }
</script>  
Ram
  • 504
  • 3
  • 11