Asfak's Blog

With jQuery write in one input field and that will fill another input field unless @ is typed.

Advertisements

I want to fill <input type=”text” size=”20″ class=”username”/> with whatever I write in <input type=”text” size=”20″ class=”email”/> unless I write ‘@’. If I write ‘@’ in <input type=”text” size=”20″ class=”email”/> then <input type=”text” size=”20″ class=”username”/> will stop writing text.

<input type=”text” size=”20″ class=”email”/>

<input type=”text” size=”20″ class=”username”/>

with jQuery we can do it simply


jQuery('.email').keyup(function (e) {
var index = text.indexOf('@');
if (index == -1) {
jQuery('.username').val(text);
}else if (index) {
jQuery('.username').val(text.substring(0, index));
}
});

Advertisements