Jump to content

How to Capitalise Variable


zer0uk

Recommended Posts

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 >

 

Link to comment
Share on other sites

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());
  });
});

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.