I've to make a program in javascript which would take an integer from the user it can be negative or positive but if user insert a decimal number or a character so this program should display an error message.
Can anyone help me jow to do it.

Troy III commented: An original reusable & efficient code has been given to you - you need to say "THANK YOU!" and mark your thread as "Solved!" -2

Recommended Answers

All 2 Replies

Member Avatar

look at using regular expressions for this.

var regex = /[0-9]+/ig;

    alert('you have an error');

I have not tested this code. If it doesn't work, look up how to use regular expressions in javascript, then look for a regular expression that works for your scenario.

you can use this code:

/*Troy III*/

to write your own function with alerts and so...

Or you can check this funcion out:

/*Troy III*/
      "0.0":"Integer value accepted",
      "1.0":"Decimals are not allowed!",
      "1.1":"Culd not convert to number!"
         v=Number(v); err=+(v%1!=0)+'.'+ +(v!=v); 
         return Err[err]

Manual check example"
integerCheck("1.121") //will alert >>decimals not allowed...

But your teacher might want to know -where did you get it?!! (badly)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.