?
Solved

Form Checking: Field must have minimum length

Posted on 2006-03-30
12
Medium Priority
?
1,569 Views
Last Modified: 2012-06-27
I have virtually no JS experience (I am a PHP programmer), so pardon my ignorance.

I just want some JS to check one field has at least 3 (non-whitespace) characters, if not, show an Alert Box with an error message.

Here is an extract of my JS:

<script language="javascript" type="text/javascript">
      
      function MinLengthEntry(val, len, str)
      {
            if(val.length < len)
            {
                  alert(str);
                  return false;
            }
            return true;
      }
      
      function ValidateData()
      {
            val = document.form_search.part_number.value;
            val = val.replace(/(^\s*)|(\s*$)/g);
            return MinLengthEntry(val, 3, "You must enter at least 3 characters in the Part Number field!");
      }
      
</script>

The Submit button code is:

<INPUT TYPE="SUBMIT" NAME="search" VALUE="Search" ID="Forms Button2"  onSubmit="return ValidateData();">

Where is my error?
0
Comment
Question by:Calvin Close
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 10

Assisted Solution

by:radarsh
radarsh earned 1200 total points
ID: 16335053
>> val = val.replace(/(^\s*)|(\s*$)/g);

Should be

   val = val.replace(/(^\s*)|(\s*$)/g, '');


________
radarsh
0
 
LVL 3

Assisted Solution

by:Kudzullc
Kudzullc earned 400 total points
ID: 16335094
calvinclose,
I see one error right off the bat.  You can not have spaces here < ID="Forms Button2> needs to be "Forms_Button2".
And, this is a big AND.  Please be sure that all your javascript is in the same case.  This is one of the first things JS coders learn.  If you call this val = document.form_search.part_number.value;   you must make sure that these form_search.part_number match case everywhere.

I hope this helps!

gl,
Lucas
0
 
LVL 10

Accepted Solution

by:
radarsh earned 1200 total points
ID: 16335136
Also, onsubmit is done on form... Not on the submit button.

<form name="form_search" onSubmit="return ValidateData();">

________
radarsh
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16335489
How about this solution.. this one does not permit user to submit unless valid part number is entered ..

<html>
<head>
<title>Validate Part Number</title>
</head>

<body>
<script language="javascript" type="text/javascript">
function ValidatePartNumber(fld) {
      var str = fld.value;
      var value = str.replace(/^(\s{1,})|(\s{1,})$|(\s{1,})/g, '');
      fld.value = value;
      var subBtn = fld.form.Forms_Button2;
      subBtn.disabled = true;
      if (value.length >= 3) {
            subBtn.disabled = false;
      }

}
</script>

<form>
Part Number : <input type="text" value="" onKeyUp="ValidatePartNumber(this);" title="At least 3 character long">
<INPUT TYPE="SUBMIT" NAME="search" VALUE="Search" name="Forms_Button2" ID="Forms_Button2" disabled="disabled">
</form>

</body>
</html>
0
 
LVL 3

Expert Comment

by:Kudzullc
ID: 16335693
Great Idea PR!

calvinclose,

Do you want to remove whitespace?

fld.value = value; ?

Lucas
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 16335871
here's a script that will validate that there is at least 3 non space chars in the field, and show an alert if the criteria is not met...

On this as well, the form will not submit until that field has the required minimum three non space chars.
You can actually click the submit, but it will not submit until requirements are met in the text field.

<html>
<head>
<title>Validate Part Number</title>
</head>

<body>
<script language="javascript" type="text/javascript">
function ValidatePartNumber(){
      var obj=document.getElementById("partNumber").value.replace(/\s/g,"");
      if(obj.length<3){
            alert("You must enter at least 3 characters in the Part Number field!");
            return false;
      }return true;
}
</script>

<form name="testingForm" action="" method="post" onSubmit="return ValidatePartNumber();">
Part Number :
<input type="text" id="partNumber" name="partNumber" title="At least 3 character long">
<INPUT TYPE="SUBMIT" NAME="search" VALUE="Search" NAME="Forms_Button2" ID="Forms_Button2">
</form>

</body>
</html>
0
 

Author Comment

by:Calvin Close
ID: 16336329
Great!  It is now working.  One last question:

I have a global_constants.php file that is included.  I want to be able to declare a constant in that file that will determine the minimum length (instead of just using '3').

1. How do I declare the constant?
2. How do I change this line so that it works?
  return MinLengthEntry(val, 3, "You must enter at least 3 characters in the Part Number field!");


Thanks,
0
 
LVL 20

Assisted Solution

by:trailblazzyr55
trailblazzyr55 earned 400 total points
ID: 16336479
in your PHP file being included you could set a "var" within <script> tags, then the javascript on your page would pick it up..

<included PHP file>
------------------------
<script>
var MyConstant = " [php value or any value you want] ";
</script>
-----------------------

<On your Page>
--------------------------
<script language="javascript" type="text/javascript">
function ValidatePartNumber(){
     var obj=document.getElementById("partNumber").value.replace(/\s/g,"");
     if(obj.length < MyConstant ){
          alert("You must enter at least "+MyConstant+" characters in the Part Number field!");
          return false;
     }return true;
}
</script>
--------------------------
0
 

Author Comment

by:Calvin Close
ID: 16336831
Thanks everyone for your help!
0
 
LVL 3

Expert Comment

by:Kudzullc
ID: 16336870
np,
Lucas
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 16336899
Glad we could help, good luck ;o)

~trail
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16339718
:)

________
radarsh
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question