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