zer0uk Posted April 14, 2020 Share Posted April 14, 2020 Hi can anyone help with this please ? I want the below script to Capitalise just the first letter of each name .. so make james brown into James Brown really appreciate any help as this is driving my mad. <script type="text/javascript"> $().ready( function() { $("#firstname ").change( function() { makeUsername() }) $("#surname").change( function() { makeUsername() }) }) function makeUsername() { var c = " " var b = $("#surname").val().substring(15,0).toLowerCase() var a = $("#firstname").val().substring(15,0).toLowerCase() $("#username").val(a+c+b) } </script> Quote Link to comment https://forums.phpfreaks.com/topic/310578-how-to-capitalise-variable/ Share on other sites More sharing options...
requinix Posted April 15, 2020 Share Posted April 15, 2020 Here's part of a regular expression-based find and replace for you: function capitalizeWords(str) { return str.replace(/(^|[\s.-])([a-z])/g, function(???) { return ???; }); } It checks for all letters at the beginning of the string, or after spaces, periods, or hyphens, then gives each result to a function. Your tasks are to: 1. Fill in the arguments. The first argument is the whole thing matched, the second is the space or period or whatever that was matched before the letter, and the third is the letter to capitalize. 2. Return the string to replace at that position. Note that it will replace the entire string matched, not just the letter. Quote Link to comment https://forums.phpfreaks.com/topic/310578-how-to-capitalise-variable/#findComment-1576873 Share on other sites More sharing options...
zer0uk Posted April 15, 2020 Author Share Posted April 15, 2020 Sorry, thanks for the help but I don't fully follow .. (to new to all this and a lot to take in) I just want to Capitalize the input field when the user types in their name ... <label for="firstname"> <i class="fas fa-user"></i> </label> <input type="text" name="firstname" placeholder="Firstname" id="firstname" required > Quote Link to comment https://forums.phpfreaks.com/topic/310578-how-to-capitalise-variable/#findComment-1576894 Share on other sites More sharing options...
TrueMember Posted April 15, 2020 Share Posted April 15, 2020 I think this is the result intended. HTML: <label for="firstname"> <i class="fas fa-user"></i> </label> <input type="text" name="firstname" placeholder="Firstname" id="firstname" required > <input type="text" name="surname" placeholder="Surname" id="surname" required > <br> <label id="result"></label> JQUERY: $(document).ready( function() { $("#firstname, #surname").keyup( function() { var firstname = $("#firstname").val($("#firstname").val().toUpperCase()); var surname = $("#surname").val($("#surname").val().toUpperCase()); $("#result").html(firstname.val() + " " + surname.val()); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/310578-how-to-capitalise-variable/#findComment-1576904 Share on other sites More sharing options...
zer0uk Posted April 15, 2020 Author Share Posted April 15, 2020 Thanks sadly that made everything go to caps.. However I have fixed it , I revisited the code I was using to make the username and tweaked it (it probably rubbish but its doing the trick) new code is <script type="text/javascript"> $().ready( function() { $("#firstname ").change( function() { makeUsername() }) $("#surname").change( function() { makeUsername() }) }) function makeUsername() { var c = " " var b = $("#surname").val().substring(0, 1).toUpperCase() var bb = $("#surname").val().substring(1, 15).toLowerCase() var a = $("#firstname").val().substring(0, 1).toUpperCase() var aa = $("#firstname").val().substring(1, 15).toLowerCase() $("#username").val(a+aa+c+b+bb) } </script> Quote Link to comment https://forums.phpfreaks.com/topic/310578-how-to-capitalise-variable/#findComment-1576907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.