Title: Form validation in javascript - minimum characters
Tags: javascript;forms
Question: I need to validate a form and I want every input must have 6 more than characters.
The following script is working for 0 characters.
How do I edit it in order to set a minimum characters limit to 6?
<script&gt;
function validateForm(formId)
var inputs, index;
var form=document.getElementById(formId);
inputs = form.getElementsByTagName('input');
for (index = 0; index < inputs.length; ++index) {
// deal with inputs[index] element.
if (inputs[index].value==null || inputs[index].value=="")
{
alert("Field is empty");
return false;
}
}
Here is the form:
<form name="myForm" id="myForm" method="post" action="" onsubmit="return validateForm('myForm');" style="margin-bottom:10px;" _lpchecked="1"&gt;
<div id="InputsWrapper"&gt;
<div&gt;
<input type="text" name="mytext[]" id="field_1" value="http://"&gt;
<a href="#" id="AddMoreFileBox" style="font-size:12px;margin:0 0 0px 0;"&gt;<img src="images/plus.png" style="margin-left:10px;height:24px;"&gt;
</a&gt;
</div&gt;
</div&gt;
<div style="height:10px;"&gt;</div&gt;
<input type="submit" style="border:none;background-color:#ff9900;padding:10px 30px 10px
30px;font-size:24px;font-weight:bold;color:#FFF;" value="CONTINUE"&gt;
</form&gt;
Comment: please format your html before you post is. using http://jsbeautifier.org/. I format your codes.

Comment: `if(inputs[index].value.length <6)`

Here is the accepted answer: Inside the loop, check the value length
if (inputs[index].value==null || inputs[index].value=="" || inputs[index].value.length < 6) {
alert("Field is not 6 characters minimum!");
return false;

Here is another answer: You can check if a field has 6 characters using:
if (document.forms["name"]["field name"].value < 6).

Here is another answer: I would suggest assigning a CSS class ("InputClassName" in this example) to the inputs you want to validate against - and then changing your code to something like this:
inputs = form.getElementsByClassName('InputClassName');
for (index = 0; index < inputs.length; ++index) {
if (inputs[index].value==null || inputs[index].value=="" || inputs[index].value.length < 6))
{
alert("Field must be 6 characters or more");
return false;
}
The advantage of this approach is that you can add radio buttons or checkboxes WITHOUT the "InputClassName" class - that have values less than 6 characters and your logic will still work.  With the current setup, your logic would break.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/17/14', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<javascript><forms>', 'Title': 'Form validation in javascript - minimum characters', 'ViewCount': 5222, 'id': 26430716, 'user_AboutMe': None, 'user_AccountId': 4799087.0, 'user_DisplayName': 'Fede Rossi', 'user_Reputation': 69.0, 'user_Views': 15.0, 'user_id': 3876180.0}}