I have two input field. one with name and other is verify name. I want to add some validation into it. If my first input contains ‘robert’ then user only can type “robert” in next field. But it is not giving me the desire result
<head>
<script type="text/javascript">
$(function(){
var jn= [];
$('#first').keypress(function(e){
var d= e.charCode || e.keyCode;
jn.push(d)})
var jm=[];
$('#second').keypress(function(e){
var f= e.charCode || e.keyCode;
jm.push(f)
for(i=0;i<jn.length;i++){
for(z=0;z<jm.length;z++)
{
if(jn[i]==jm[z]){
alert('hiii')}}}})
})
</script>
</head>
<body>
name
<input type="text" id="first" />
verify name
<input type="text" id="second" />
</body>
I believe you’re over thinking it. Your solution also doesn’t lend itself to editing (due to having to track the array of keycodes). Try something like this
http://jsfiddle.net/qccQZ/3/