Mahmoud Mahmoud - 9 months ago 25
Javascript Question

javaScript - number input - accept specific numbers

I have 5 inputs with type 'number'. max value to be entered by the user is '5'.

I want if the user enter for example '4' in the first input field . he can't submit this number again in the other fields.

or he can but then an message pop up to alert him.

it 'd be great if the alert is built in the browser like when he enter a number that more than the 'max' the field get red as a wrong assignment.

I can make an array and when the user submit an value I check if that value in the array and if not I push it in the array
but then I 'll have to make an alert design.

I just looking for smart and easy solution.

<input type='number' max = '5' onchange = 'change(this)'>
<input type='number' max = '5' onchange = 'change(this)'>
<input type='number' max = '5' onchange = 'change(this)'>
<input type='number' max = '5' onchange = 'change(this)'>
<input type='number' max = '5' onchange = 'change(this)'>

function change(it)
{
// what shoult I do ?!...

}


thanks.

Answer Source

You can use jquery and compare value of fields with each other upon change. See this answer:

http://stackoverflow.com/a/30307040/1333744