username page javascript validation

Could anyone help with this?

Code allowing the user to input a username and a password, both of which must be validated as follows:

The username may only contain letters, dots (.), the at sign (@). The username must NOT be longer than 25 characters.



answered 3 months ago boeledi #1

In order to limit to 25 characters, the easiest way is to use an

<input type="text" maxlength="25" />

In order to validate that input only contains letters, dots (.) and @, proceed with a regular expression.


<input id="input" type="text" maxlength="25" />
<button id="button">Test</button>
<div id="validation" />

   var $text = $('#input');
   var $btn  = $('#button');
   var $out  = $('#validation');

   $btn.on('click', _do_check);

  function _do_check(e){

    var text = $text.val();

    if (/^[[email protected]]+$/.test(text) ){
    } else {

Hope this helps.

answered 3 months ago jmtalarn #2

Using only plain Javascript:

You will need to construct a regular expression. Check this to get an idea how it works in Javascript

The following expression would be a regular expression fitting your username requirements with the only allowed characters and the length restrictions at once


You can play and learn how to build a regular expression with pages like

And as recommended you should start with some of your work in order to help actually with a problem and then the community can guide you to solve your real issue.

comments powered by Disqus