-2

I am trying to create a system username that consists of the first alphabetic characters found in the Family name, street address, Given name; the numerical day of the month; and the numerical seconds field of the time of submission. At the moment i have this below, it works without the all the address code (gname and surname).

function validateForm()
 {
    var system= '';
    var givenname= document.getElementById('gname').value;
    var familyname= document.getElementById('surname').value;
    var addy= document.getElementById('address').value;
    addy = addy.replace(/[0-9]/g, "");


    var givchar = givenname.substr(0, 1);
    var famchar = familyname.substr(0, 1);
    var addchar = addy.substr(0, 1);

    system += famchar+givchar+addchar;

    document.getElementById('susername').value=system;
}

If I remove the following:

var addy= document.getElementById('address').value;
              addy=addy.replaceAll("[0-9]","");
              var addchar = addy.substr(0, 1);

Then the correct indexes are extracted, otherwise at the moment nothing is displayed in the susername text box.

 <form id="rego" action="<?php echo 
    htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="return validateForm()">

    <label>Given Name:</label> <input type="text" id="gname" name="gname"><br />
    <br />
    <label>Middle Name: </label><input type="text" name="mname"><br />
    <br />
    <label>Family Name:</label> <input type="text" id="surname" name="surname"><br />
    <br />
    <label>Chosen Username:</label> <input type="text" name="username"><br />
    <br />
    <label>Address:</label> <input type="text" id="address" name="address"><br />
    <br />
    <label>Postcode: </label><input type="text" name="postcode"><br />
    <br />
    <label>State:</label> <input type="text" name="state"><br />
    <br />
    <label>Tel number: </label><input type="text" name="tel"><br />
    <br />
    <label>Password:</label> <input type="password" name="password" value="<?php 
    echo $passw;?>"/><span class="error">* <?php echo $passErr;?></span><br />
    <br />
    <label>Password confirmation:</label> <input type="password" name="passconfirm" value="<?php 
    echo $passw1;?>"/><span class="error">* <?php echo $pass1Err;?></span><br />
    <br />
    <label>System username</label> <input type="text" name="susername" id="susername" >
    <br />
    <input type="submit" value="submit" name="submit">
    </div>
</form>
  • I think you want `addy=addy.replaceAll(/[0-9]/,"");` to remove all digits from the address value. – Pointy May 29 '14 at 17:54
  • This would seem to imply that there is an error on your javascript console. Have you checked? – James Montagne May 29 '14 at 17:56
  • @Pointy I tried your code to no success. – user3688617 May 29 '14 at 18:00
  • 2
    There's a quite [similar question](http://stackoverflow.com/q/23932125/1169519), yours? With a new account? – Teemu May 29 '14 at 18:00
  • I might regret asking this but I'm curious... Why are you generating a hard to remember username like this? – Basic May 29 '14 at 18:01
  • @James Montagne what should i check exactly? All i know that the form is submitting and the susername value is not changing in the text box – user3688617 May 29 '14 at 18:01
  • 1
    @user3688617 Check the JS console for error messages describing the problem... – Basic May 29 '14 at 18:02
  • @Basic it is required of me for a project. This system username is meant to be hidden input, but I have made the type to be text so i can see if it is working correctly – user3688617 May 29 '14 at 18:03
  • 1
    @user3688617 See [this](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers) for how to check your console. If there is a js error it will show here. I believe you will find something like "replaceAll is not a function". Because js doesn't have `replaceAll`, you have to use `replace`. – James Montagne May 29 '14 at 18:03
  • @JamesMontagne boy I'm dumb sometimes :) – Pointy May 29 '14 at 18:04
  • Yeah... echoing back the user's password in the HTML? Probably the worst possible thing you could ever do. – Sammitch May 29 '14 at 18:05
  • @Basic i am using notpad and uploading my work onto a private server then running a single php file. Im not sure how to check the JS console. Sorry lol – user3688617 May 29 '14 at 18:05
  • @user3688617 the JavaScript console is built into the web browser you're using to test the code. Hit the `F12` key. – Pointy May 29 '14 at 18:05
  • @Pointy Happens to the best of us. – James Montagne May 29 '14 at 18:07
  • 2
    And also [this deleted one](http://stackoverflow.com/q/23937217/1169519)? – Teemu May 29 '14 at 18:09

1 Answers1

2

Try changing

addy=addy.replaceAll("[0-9]","");

with

addy = addy.replace(/[0-9]/g, "");

Or, to get the first letter (not number, symbol, etc.), use:

addy = addy.replace(/[^A-Za-z]/g, "");

Good luck!

  • hey pointer thank you it now goes through, however only the first index for strings gname and surname are shown. I need it to shave the numbers off(which is what we did with replace) and show the first character of the street name e.g 123 wood street would display 'w' – user3688617 May 29 '14 at 18:11
  • @user3688617, I've edited my answer above. I guess that will do what you need, only getting letters. – Alexandre R. Janini May 29 '14 at 18:16