Javascript validation for number of characters

Asked
Active3 hr before
Viewed126 times

5 Answers

numbervalidationcharactersjavascript
90%

I need to validate an input (password field) with Javascript / jQuery, What's needed for a solar powered past?

Example_snippet/controller/utility/_number.js/ var p = jQuery('#passwordfield. . .
var p = jQuery('#passwordfield').val();
if (p.length <= 7 || p.length >= 33 || !p.match(/\d/) || !p.match(/[a-z]/i))
   $('.whatever').addClass('error');
load more v
88%

Don't tell someone to read the manual, Chances are they have and don't get it

Example_snippet/controller/utility/_number.js/ function Validate() { . . .
function Validate() {
   var inputString = 'DE55680501010022061234';
   // using regex
   var regEx = /DE\d{20}$/;
   if (regEx.test(inputString) == false) {
      alert('Not right!')
   }

   // without regex
   if (inputString.length != 22) {
      alert('Not right!')
   }

   if (inputString.charCodeAt(0) != 68 || inputString.charCodeAt(1) != 69) {
      // not D or E
      alert('Not right!')
   }

   for (var i = 2; i < 22; i++) {
      if (!(inputString.charCodeAt(i) > 47 && inputString.charCodeAt(i) < 58)) {
         alert('Not right!')
      }
   }
}
load more v
72%

Sometimes situation arises when a field in an html form accept a restricted number of characters, For example a userid (length between 6 to 10 character) or password (length between 8 to 14 characters)

Example_snippet/controller/utility/_number.js/ function lengthRange(inputtxt,. . .
function lengthRange(inputtxt, minlength, maxlength) {
   var userInput = inputtxt.value;
   if (userInput.length >= minlength && userInput.length <= maxlength) {
      return true;
   } else {
      alert("Please input between " + minlength + " and " + maxlength + " characters");
      return false;
   }
}
Step 2 continued with <!DOCTYPE html> <html lang="en. . .
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="utf-8">
   <title>JavaScript form validation - checking non empty</title>
   <link rel='stylesheet' href='form-style.css' type='text/css' />
</head>

<body onload='document.form1.text1.focus()'>
   <div class="mail">
      <p>Enter Userid [between 6 to 8 characters] and Submit</p>
      <form name="form1" action="#">
         <ul>
            <li>Username:<input type='text' name='text1' /></li>
            <li>&nbsp;</li>
            <li class="submit" it"><input type="submit" name="submit" value="Submit" onclick="stringlength(document.form1.text1,6,8)" /></li>
            <li>&nbsp;</li>
      </form>
      </ul>
   </div>
   <script src="string-lenght.js"></script>
</body>

</html>
Step 3 continued with function stringlength(inputtxt. . .
function stringlength(inputtxt, minlength, maxlength)
{ 
var field = inputtxt.value; 
var mnlen = minlength;
var mxlen = maxlength;

if(field.length<mnlen || field.length> mxlen)
{ 
alert("Please input the userid between " +mnlen+ " and " +mxlen+ " characters");
return false;
}
else
{ 
alert('Your userid have accepted.');
return true;
}
}

Step 4 continued with li {list-style-type: none; fon. . .
li {
   list - style - type: none;
   font - size: 16 pt;
}
.mail {
   margin: auto;
   padding - top: 10 px;
   padding - bottom: 10 px;
   width: 400 px;
   background: #D8F1F8;
   border: 1 px soild silver;
}
.mail h2 {
   margin - left: 38 px;
}
input {
   font - size: 20 pt;
}
input: focus, textarea: focus {
   background - color: lightyellow;
}
input submit {
   font - size: 12 pt;
}
.rq {
   color: #FF0000;
   font - size: 10 pt;
}
load more v
65%

Example_snippet/controller/utility/_validation.js/ function lengthRange(inputtxt,. . .
function lengthRange(inputtxt, minlength, maxlength) {
   var userInput = inputtxt.value;
   if (userInput.length >= minlength && userInput.length <= maxlength) {
      return true;
   } else {
      alert("Please input between " + minlength + " and " + maxlength + " characters");
      return false;
   }
}
75%

String length validation in JavaScript is used to make sure that number of characters entered in a specified field will be restricted, For example name field should have a length of 8-10 characters only

Example_snippet/controller/utility/_validation.js/ <!DOCTYPE html> <html lang="en. . .
<!DOCTYPE html>
<html lang="en">
<head>
<script> 
function stringLengthCheck(name, minlength, maxlength)
{
var mnlen = minlength;
var mxlen = maxlength;
 
if(name.value.length<mnlen || name.value.length> mxlen)
{ 
alert("Name should be " +mnlen+ " to " +mxlen+ " characters.");
return false;
}
else
{ 
return true;
}
}    
</script> 
</head>
<body>
<div class="mail">
<h2>JavaScript String Length Validation</h2>
<form name="form1" action="#"> 
Name: <input type='text' name='name'/></br></br>
<input type="submit" name="submit" value="Submit" 
onclick="stringLengthCheck(document.form1.name, 8, 10)"/>
</form>
</div>
</body>
</html>