-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.

4
  • 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: stackoverflow.com/questions/1221985/… Commented Apr 17, 2018 at 10:25
  • You can either use a custom library like jQuery Validate jqueryvalidation.org/validate or learn how to create validation using REGEX Commented Apr 17, 2018 at 10:27
  • I also just have to mention that your name is quite ironic Commented Apr 17, 2018 at 10:28
  • Please. Upload some snippet showing but you have done so far. Commented Apr 17, 2018 at 10:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.