-1

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.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • I'm afraid you cannot ask a question which shows no previous research or effort, you are essentially asking people to do your work for you, and you will learn nothing by doing this. Read this: https://stackoverflow.com/questions/1221985/how-to-validate-a-user-name-with-regex – Panomosh Apr 17 '18 at 10:25
  • You can either use a custom library like jQuery Validate https://jqueryvalidation.org/validate/ or learn how to create validation using REGEX – Panomosh Apr 17 '18 at 10:27
  • I also just have to mention that your name is quite ironic – Panomosh Apr 17 '18 at 10:28
  • Please. Upload some snippet showing but you have done so far. – goediaz Apr 17 '18 at 10:53

2 Answers2

0

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.

Example:

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


$(document).ready(function(){
   var $text = $('#input');
   var $btn  = $('#button');
   var $out  = $('#validation');

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

  function _do_check(e){
    e.preventDefault();
    e.stopPropagation();

    var text = $text.val();

    if (/^[a-zA-Z.@]+$/.test(text) ){
      $out.html('OK');
    } else {
      $out.html('FAILURE');
    }    
  }
});

Hope this helps.

boeledi
  • 6,837
  • 10
  • 32
  • 42
0

Using only plain Javascript:

You will need to construct a regular expression. Check this to get an idea how it works in Javascript https://www.w3schools.com/jsref/jsref_obj_regexp.asp

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

/^([a-zA-Z\.\@]{1,25})$/

You can play and learn how to build a regular expression with pages like https://regexr.com/

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.

jmtalarn
  • 1,513
  • 1
  • 13
  • 16