Link to home
Start Free TrialLog in
Avatar of trailerman
trailerman

asked on

Require minimum number of characters in form

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>

ASKER CERTIFIED SOLUTION
Avatar of ahosang
ahosang
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trailerman
trailerman

ASKER

It doesn't work.... how can it find "table.company" when you have "table_company"?
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.
still don't work. did you test it?
Also, want to be able to specify the table. Do you know how to write the "." as an element?
Show your full code that you are testing please
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>
not working yet. thanks for trying
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:-)
<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>
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>
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)">
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
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.
trailerman ..
          Have u checked my code ...it's working i tested it on IE5 , n have "." in table.company....

Regards.
lk
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.
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
so your advise is to..... as related to the points.?
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
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.
trailerman,
You decide urself who shld get the points if u think it shld split then split the points..

Regards,
lk:-)
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 https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20152494 "

Cheers

modder
Community Support Moderator@Experts-Exchange
trailerman..
           cAN I ASK IS NO POINTS FOR ME...???
Regards,
lk
Yes, of course. I apologize for missing the last step... the splitting of points is new process to me.

Thanks again for you help.
traileman,
        I m still waiting ....
lk.
It was put a few days ago at:

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

If you post a "hello" for answer, I'll accept it.
trailerman..
      blank answer posted.. waiting for ur response..
Thanx n Regarss.
:)