[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Require minimum number of characters in form

Posted on 2001-07-17
27
Medium Priority
?
244 Views
Last Modified: 2013-12-03
I need  javascript that can do the following with an html search form:

Require that at least three characters are used in search form.
 If user trys to search with less than three characters in search field, then
 Popup javawindow appears that says "Please be more specific with search criteria", and
form will not perform search.


Here issimple  the form:
<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
??your help??
</script>
 </HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST>
   <P>
<B>Name</B><INPUT TYPE=text NAME="table.company" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search"></FONT>
</FORM>
</BODY>
</HTML>

0
Comment
Question by:trailerman
  • 10
  • 7
  • 4
  • +3
27 Comments
 
LVL 12

Accepted Solution

by:
ahosang earned 150 total points
ID: 6291976
<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function check(formObj) {
  if(formObj.table_company.value.length<3) {
    alert("Please be more specific with search criteria");
    return false;
  } else {
    return true;
  }
}
</script>
</HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST onsubmit="return check(this)">
  <P>
<B>Name</B><INPUT TYPE=text NAME="table_company" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search"></FONT>
</FORM>
</BODY>
</HTML>

I changed the textfield name to table_company because table.company may cause problems with javascript.
0
 

Author Comment

by:trailerman
ID: 6291992
It doesn't work.... how can it find "table.company" when you have "table_company"?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6292012
Then change this bit:
if(formObj.table_company.value.length<3) to

if(formObj.elements[0].value.length<3)

Then your textfield will have to be the first form element.
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!

 

Author Comment

by:trailerman
ID: 6292017
still don't work. did you test it?
0
 

Author Comment

by:trailerman
ID: 6292019
Also, want to be able to specify the table. Do you know how to write the "." as an element?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6292021
Show your full code that you are testing please
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6292121
use elements

if this works, points to ahosang


<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function check(formObj) {
 if(formObj.elements['table.company'].value.length<3) {
   alert("Please be more specific with search criteria");
   return false;
 } else {
   return true;
 }
}
</script>
</HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST onsubmit="return check(this)">
 <P>
<B>Name</B><INPUT TYPE=text NAME="table.company" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search"></FONT>
</FORM>
</BODY>
</HTML>
0
 

Author Comment

by:trailerman
ID: 6292207
not working yet. thanks for trying
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6292387
Check this......

<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function checkform(){
if (document.forms["searchform"].elements["table.company"].value.length<3){
     alert("Please be more specific with search criteria");
     return false;
}
else{
     return true;
}

}
</script>
</HEAD>
<BODY>
<FORM name="searchform" METHOD="POST" onSubmit="return checkform()" ACTION="/cgi-bin/sql/search.cgi">
  <P>
<B>Name</B><INPUT TYPE=text NAME="table.company" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search">
</FORM>
</BODY>
</HTML>

Hope it will help..

Regards,
lk:-)
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6292631
<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function formSubmit(fObj)
{
   if(fObj["tablecompany"].value.length<3) return false;
   return true;
}
</script>
</HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST onSubmit="return formSubmit(this)">
  <P>
<B>Name</B><INPUT TYPE=text NAME="tablecompany" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search"></FONT>
</FORM>
</BODY>
</HTML>
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6292667
or

<HTML>
<HEAD><TITLE>Search Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function formSubmit(fObj)
{
   for(i=0;i<fObj.length;i++)
      if(fObj[i].name=="table.company" && fObj[i].value.length<3) return false;
   return true;
}
</script>
</HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST onSubmit="return formSubmit(this)">
  <P>
<B>Name</B><INPUT TYPE=text NAME="table.company" SIZE=10 MAXLENGTH=60>

<INPUT TYPE=submit VALUE="Search"></FONT>
</FORM>
</BODY>
</HTML>
0
 

Author Comment

by:trailerman
ID: 6294874
Nope.  don't forget I need the alert, and that the "." is taken into consideration. I want the javascript to work with my table/field requirements, and not the opposite approach.  I increased to 100 points, please only send forms that you have tested. I assume that you can test this by putting less than on character in the field.  SHOULD this be an ONCLICK function vs. onsubmit, so the field alert can be tested first before form/search function?
I believe, this doesn't have anything to do with the search/onsubmit per se (i believe again), just what is typed in the field.... Is there less or greater than three characters, If less than, return alert, if same or greater, then allow submit (or ignore javafuction)

thanks again in advance.
here is sample that considers alerts and table.field format....it just doesn't work either.


SCRIPT LANGUAGE="JavaScript">
function check(formObj) {
if(formObj.elements['table.company'].value.length<3) {
  alert("Please be more specific with search criteria");
  return false;
} else {
  return true;
}
}
</script>
</HEAD>
<BODY>
<FORM ACTION="/cgi-bin/sql/search.cgi" METHOD=POST onsubmit="return check(this)">
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6294989
trailerman ..
  don't get u.."I want the javascript
to work with my table/field requirements, and not the opposite approach." ,can u explain this further..


Regards,
lk
0
 

Author Comment

by:trailerman
ID: 6295176
sorry for confusion.,just wanted to make sure everyone understood (which I see you do from your answer) that I need the "." between table.company to remain to go along with my db table/field structure.  however,  the main point is that the javascript doesn't work.  
what am I missing that would help you folks out. thanks for your patience and contributions.
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6295289
trailerman ..
          Have u checked my code ...it's working i tested it on IE5 , n have "." in table.company....

Regards.
lk
0
 

Author Comment

by:trailerman
ID: 6295595
I checked, but now realized that ahosang answered worked as well. Do I split the points as  lokeshv gave more specific answer? and if so, is this possible.

I profusely apologize for not being able to verify that they worked earlier...eventhough  I tested each one the same way I did now(?)

please advise on point split question..and again apologize.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6296182
ahosang answered first,


but lokeshv's answer (which incedentally is the SAME as mine)

is the most technically correct.

like we both pointed out, the is the purpose of the elements collection.


Bob
0
 

Author Comment

by:trailerman
ID: 6296248
so your advise is to..... as related to the points.?
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6296300
well, there are two schools

I would give the points to ahosang because he answered first...

the other schools says give the points to the answer you use, in which case, I would expect that to be lokeshv's

mainly the previous post was to point out that using the elements array is really the way to do this...

it's up to you.

if you want, you can post a q in community support, and you can split the points.

speaking for them, I don't think either ahosang or lokeshv would be insulted, as long as one of them got the points!

Bob
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6296364
You choose trailerman, if you want to split just post a zero point question in the Community Support Area with a link to this question and say who you want to split between.
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6296702
trailerman,
You decide urself who shld get the points if u think it shld split then split the points..

Regards,
lk:-)
0
 
LVL 3

Expert Comment

by:modder
ID: 6298027
Hello trailerman,

I've halved the points on this question, from 100 to 50

You can award this to one of the two experts now.

For the other experts, simply post a dummy question titled "Points for <the other expert's name>"

And say something like "For your help with http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20152494 "

Cheers

modder
Community Support Moderator@Experts-Exchange
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6301403
trailerman..
           cAN I ASK IS NO POINTS FOR ME...???
Regards,
lk
0
 

Author Comment

by:trailerman
ID: 6304937
Yes, of course. I apologize for missing the last step... the splitting of points is new process to me.

Thanks again for you help.
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6317241
traileman,
        I m still waiting ....
lk.
0
 

Author Comment

by:trailerman
ID: 6317818
It was put a few days ago at:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=javascript&qid=20154810

If you post a "hello" for answer, I'll accept it.
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 6376989
trailerman..
      blank answer posted.. waiting for ur response..
Thanx n Regarss.
:)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

873 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