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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Min. # of Characters in a form field 1

Status
Not open for further replies.

Berns

IS-IT--Management
Apr 25, 2002
41
US
i need help with 2 things -
1. i have a a form w/a text field, i want the users input to be 6 characters, no more no less

2. same form, not every field is required but when the user hit submit i want an alert saying that the "entire form has not been completed do you want to proceed yes/no?"

can anyone get me started?
 
first question you can do a onBlur in the text field to check for .length of the field and alert as needed

second question
onSubmit loop through all the form field and upon recieving one that is a emtpy value send the alert. use a confirm in that conditioning to give the the chance to stop the submission and if confirm then return false to stop the process for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
here's a cheesee example
<html>
<head>
<script>
function sixChk(obj) {
if (obj.value.length!=6) {
alert(&quot;you must have 6 cahracters in this field&quot;);
obj.focus();
}
}

function checkForm() {
var frm = document.forms[0];
for(var x =0; x < frm.length; x++) {
if(frm.elements[x].value == &quot;&quot;) {
alert(&quot;need a value in field &quot;+frm.elements[x].name);
return false;
}
}
}
</script>
</head>
<body>
<form name=&quot;frm&quot; onSubmit=&quot;return checkForm()&quot; method=&quot;get&quot;>
six characters only <input type=&quot;text&quot; name=&quot;sixChar&quot; onBLur=&quot;sixChk(this)&quot;><br>
others<br>
<input type=&quot;text&quot; name=&quot;txt2&quot;><br>
<input type=&quot;text&quot; name=&quot;txt3&quot;><br>
<input type=&quot;text&quot; name=&quot;txt4&quot;><br>
<input type=&quot;text&quot; name=&quot;txt5&quot;><br>
<input type=&quot;text&quot; name=&quot;txt6&quot;><br>
<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>
</body>
</html> for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
I am currently using onBlur to make sure that that field is complete, can I add something to this script to check that the ID is 6 digits? Otherwise how would I use 2 onBlur? here is the code:
function checkRequired( theForm )
{
var bMissing2 = false;
if ( 0 == theForm.Q15.value )
bMissing2 = true;

if ( bMissing2 )
{
alert( &quot;Please enter your six digit ID number.&quot;);
theForm.Q15.focus();
return false;
}
else
{
return true;
}
}
 


1. <input type=&quot;text&quot; name=&quot;xxx&quot; id=&quot;yyy&quot; size=&quot;10&quot; maxlength=&quot;10&quot;>

 
I tried replacing
if ( bMissing2 )
{
alert( &quot;Please enter your six digit ID number.&quot;);

with

if (theForm.Q15.value.length<>6)
{
alert( &quot;Please enter your six digit ID number.&quot;);

And it did not work, is that what you are instructing me to do?
 
That one worked perfectly, thanks!

in terms of validating the form, I currently have the onsubmit set as follows:
onsubmit=&quot;return checkRequired(this)&quot;

How can I implment the script that you provided above:
onSubmit=&quot;return checkForm()&quot;

Can I just substitute:
function checkForm()
to this
function checkRequired()
??
 
no, you can't add the two function to call in the onSubmit at least easily.
just add the script to the function you already have after the other conditional terms you state have been met.

let us know if you ahve problems getting them to talk right for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
It didn't work, this is what I have:

function checkRequired( theForm )
{
var bMissing2 = false;
if ( 0 == theForm.Q15.value )
bMissing2 = true;

if (theForm.Q15.value.length!=6)
{
alert( &quot;Please enter your six digit GID.&quot;);
theForm.Q15.focus();
return false;
}
else
{
return true;
}
//your code
var frm = document.forms[0];
for(var x =0; x < frm.length; x++) {
if(frm.elements[x].value == &quot;&quot;) {
alert(&quot;need a value in field &quot;+frm.elements[x].name);
return false;
}
}
}

 
try this I may have missed a }
function checkRequired(theForm) {
var frm = document.forms[0];
var bMissing2 = false;
for(var x =0; x < frm.length; x++) {
if(frm.elements[x].value == &quot;&quot;) {
alert(&quot;need a value in field &quot;+frm.elements[x].name);
return false;
}
}
if ( 0 == theForm.Q15.value )
bMissing2 = true;
if (theForm.Q15.value.length!=6) {
alert( &quot;Please enter your six digit GID.&quot;);
theForm.Q15.focus();
return false;
} else {
return true;
}
} for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
It looks like that would work, the problem is that I have a bunch of hidden fields and it is picking those up and requesting a value, Urg!

Any ideas?

Thanks for all of your help!!
 
actually a small fix as long as you have a set naming convention for your input's
for(var x =0; x < frm.length; x++) {
if(frm.elements[x].name.indexOf('txt')!==-1){
if(frm.elements[x].value == &quot;&quot;) {
alert(&quot;need a value in field &quot;+frm.elements[x].name);
return false;
}

you'll see from my firs example of this that I named my inputs txt2 txt3 etc.. the indexOf function will check for the instance of a set pattern (in this case txt) and as set in thename of the element that is currently being focus'd on. so if this condition is not met then you skip over the next condition and move to the next field.

you can also reverse this process to skip over any hidden form fields as lons as there is a common part in the naming again. for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top