/    Sign up×
Community /Pin to ProfileBookmark

Changing Form text field from Optional to Mandatory if filled out

Hi Folks,

I have an HTML form created in Dreamweaver which has behaviours set to validate that three fields are mandatory.

I have been asked if a further three fields with ID’s of ; address1, town and postcode can be further validated to be optional if all three are left blank, but if any one is filled then they all become mandatory.

I know enough Javascript to amend existing code but struggle to code from scratch. If anybody can help it will be appreciated and please can you reply in “Idiot Speak”

Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 05.2008 — In your validation section add the following before the return
[code=php]
// added checks
var addr = document.getElementById('address1').valuel;
var town = document.getElementbyId('town').value;
var post = document.getElementById('postcode).value;

if ((addr == '') && (town == '') && (post == '')) {
// no entries found, so becomes optional
} else {
// check which entry is blank and report it
if (addr == '') { alert('Missing address'); return false; }
if (town == '') { alert('Missing town'); return false; }
if (post == '') { alert('Missing postcode'); return false; }
// if you made it this far, then all non-blank info has been entered, OK
}
/// .... more code
return true; // assuming all other information is validated as true
} // end of validation function
[/code]

Only a guess as you did not provide the validation code to review.
Copy linkTweet thisAlerts:
@bosschatNov 06.2008 — The validation code in the head is;

<script type="text/javascript">

function MM_validateForm() { //v4.0

if (document.getElementById){

var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);

if (val) { nm=val.name; if ((val=val.value)!="") {

if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');

if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';

} else if (test!='R') { num = parseFloat(val);

if (isNaN(val)) errors+='- '+nm+' must contain a number.n';

if (test.indexOf('inRange') != -1) { p=test.indexOf(':');

min=test.substring(8,p); max=test.substring(p+1);

if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';

} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }

} if (errors) alert('The following error(s) occurred:n'+errors);

document.MM_returnValue = (errors == '');

} }

</script>



And on the onClick event is;



<input name="submit" type="submit" id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail','amend_code','','R');return document.MM_returnValue" value="Submit" />

<input type="reset" name="reset" id="reset" value="Reset" />



I've tried putting your script in to this in various places and it just screwed the validation. I tried creating a new function and adding it into the onSubmit with a semi-colon seperator and it did nothing. I tried removing the MM validation and just leaving a function called test-empty() and added that to the onClick event, filling in the address1 field and it posted to an email without validating.



If you have any ideas I would welcome your thoughts. Thanks for your time.
×

Success!

Help @chaster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...