...

View Full Version : Simple Javascript Snippet needed!



JamesH
05-10-2007, 10:40 AM
Hi there,

I'm coding a webapp which has a user system where admins can add or edit users. Nothing out of the ordinary.

I have a form which looks like this:



<form method="POST">
<input type="text" name="firstname" />
<input type="text" name="lastname" />
<input type="text" name="username" />


When the user focuses the username field, I would like the firstname and lastname inserted.


For example, firstname=James lastname=H, in username it would be jamesh (converted to lowercase also).

Can you guys help me?


Thanks very much!

Bill Posters
05-10-2007, 11:27 AM
one rough e.g.


<script type="text/javascript">

function setUsername() {

var fname = document.getElementById('firstname');
var lname = document.getElementById('lastname');
var uname = document.getElementById('username');

if (fname.value != '' && lname.value != '') {
uname.value = (fname.value+lname.value).toLowerCase();
}

}

</script>

...

<input type="text" name="firstname" id="firstname" />
<input type="text" name="lastname" id="lastname" />
<input type="text" name="username" id="username" onfocus="setUsername();" />

JamesH
05-11-2007, 10:29 AM
Thanks. I had to tweak it a little, but still a great example.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum