Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form validation - how do I define the minimum amount of text

Status
Not open for further replies.

Floodster

Technical User
Joined
Jan 28, 2005
Messages
204
Hi,
I have some JAVASCRIPT which I have had help with from this forum which ensures that an entry has been entered into a postcode search. What I need now is the code slightly amending so that the user has to enter at least 3 characters before it will submit (i.e B68 or DY3), this will ensure that if the user just enters B they won't get 000's of results. I'm not very good with JAVASCRIPT so I've not got a clue where to start. The current code is;
Code:
function validate_postcode(field,alerttxt)
{
with (field)
{

if (value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form3(postcode_search)
{
with (postcode_search)
{
if (validate_postcode(postid,"Enter Postcode")==false)
  {postid.focus();return false}
}
}

Thanks.
 
>if (value=="")
[tt]if (value=="" [blue]|| value.length<3[/blue])[/tt]
 
tsuji
I have tried this & other combinations but it still accepts 1 character??
any ideas?
 
Try displaying the size of the text to get an idea of what's happening, or post your code here.

Cheers,
Dian
 
Dian,
Not quite sure what you mean when you say "display the size of the text"? & the code is what I have already posted with the inclusion of tsuji code. I will include it anyway
Code:
function validate_postcode(field,alerttxt)
{
with (field)
{

if (value=="" || value.length<3)
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form3(postcode_search)
{
with (postcode_search)
{
if (validate_postcode(postid,"Enter Postcode")==false)
  {postid.focus();return false}
}
}

cheers.
 
I mean
Code:
alert(value.length);

Cheers,
Dian
 
Also verify field is properly passed. It should be an element object. So before running into the "with (field)" line, alert its data.
[tt] alert(typeof(field)+"\n"+field.name+"\n"+field.value)[/tt]
 
Guys,
I don't think I had refreshed my browser since the changes because it is now working.
One other question is how do I put my alerts on more than 1 line, ie.

Alert this is an alert
This alert has been activated.


Thanks.
 
There are a few problems with the code as posted.
First you appear to be passing the field in as an object so that you can work with it's properties and you pass the fieldname in under the variable named postcode_search to your validate_form3 function. But when you call the validate_postcode function you refer to it by the name postid rather than your object reference variable of postcode_search.

Just change the references to postid to postcode_search and you should be fine.
Code:
function validate_postcode(field,alerttxt)
{
  with (field)
  {
    if ([COLOR=blue]value.length < 3[/color])
    {
      alert(alerttxt);
      return false
    }
    else {
      return true
    }
  }
}

function validate_form3(postcode_search)
{
  with (postcode_search)
  {
    if (validate_postcode([COLOR=blue]postcode_search[/color],"Enter Postcode")==false)
    {
      [COLOR=blue]postcode_search[/color].focus();
      return false
    }
  }
}

It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top