• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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>

0
trailerman
Asked:
trailerman
  • 10
  • 7
  • 4
  • +3
1 Solution
 
ahosangFinance Systems DeveloperCommented:
<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
 
trailermanAuthor Commented:
It doesn't work.... how can it find "table.company" when you have "table_company"?
0
 
ahosangFinance Systems DeveloperCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Regards.
lk
0
 
trailermanAuthor Commented:
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
 
bebonhamCommented:
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
 
trailermanAuthor Commented:
so your advise is to..... as related to the points.?
0
 
bebonhamCommented:
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
 
ahosangFinance Systems DeveloperCommented:
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
 
lokeshvCommented:
trailerman,
You decide urself who shld get the points if u think it shld split then split the points..

Regards,
lk:-)
0
 
modderCommented:
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
 
lokeshvCommented:
trailerman..
           cAN I ASK IS NO POINTS FOR ME...???
Regards,
lk
0
 
trailermanAuthor Commented:
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
 
lokeshvCommented:
traileman,
        I m still waiting ....
lk.
0
 
trailermanAuthor Commented:
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
 
lokeshvCommented:
trailerman..
      blank answer posted.. waiting for ur response..
Thanx n Regarss.
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 10
  • 7
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now