0

Here I want to get the first three string characters from the username input field & generate a random number first 3 characters of the username & random see output to get the expected output

    <input type="text" id="firstname" name="firstname" onBlur="?"><br>

    <?php  $ran = rand(1000, 9999); ?>

    <input type="text" value="<?php echo $ran; ?>" placeholder="username">

My Expected Output

KSA3215
  • Does this answer your question? [How to get first 5 characters from string](https://stackoverflow.com/questions/3787540/how-to-get-first-5-characters-from-string) – Will Aug 29 '20 at 10:33
  • And https://stackoverflow.com/questions/4990752/generate-an-n-digit-random-number – Will Aug 29 '20 at 10:35
  • i want to get 3 characters from input field values?? –  Aug 29 '20 at 10:36
  • https://stackoverflow.com/questions/13447554/how-to-get-input-field-value-using-php – Will Aug 29 '20 at 10:40
  • Are you fetching any value for input filed dynamicly – Kumar Praveen Aug 29 '20 at 10:43
  • just want to insert Firstname from input field --like --when put KASHIF input field then KAS will display on username field? –  Aug 29 '20 at 10:46
  • Hi i have answer the below quetion in php you can achieve it using ajax or javascript on blur method – Kumar Praveen Aug 29 '20 at 10:50

1 Answers1

0

Hi will you try below code if it fullfills your requirement.

<input type="text" id="firstname" name="firstname"><br>

<?php  
     $ran = rand(1000,9999); 
     $firstThreeChar=substr("yourstring",0,2);
     $restring=$firstThreeChar.$ran;
?>
<input type="text" value="<?php echo $restring; ?>" placeholder="username">
  • but username field get string from Firstname input field? can we change this ? user can add his/her name then username auto generate?? –  Aug 29 '20 at 10:57
  • Yes that will work only when you will use ajax . if you don want to use ajax you can done it using javascript event onblur . – Kumar Praveen Aug 29 '20 at 11:00
  • onblur event you to use the substring(1, 4); method of javascript string and Math.random mehtod. and concatenate the result – Kumar Praveen Aug 29 '20 at 11:02
  • Can you send me any link? when user put username and click tab button auto random number will generate on the Username input field ---in this I have 2 input fields First for the Name where user input his name and I want to get random values on username field when click on tab button ? Can you please send me any relevant link fo this ? –  Sep 03 '20 at 17:37