Jump to content

Validating Generated Form Elements?


Kingy

Recommended Posts

Hi guys,I've been creating this upload page which generates hidden form elements to store data in order to be submitted and processed via php.Before I got to that stage I used the gen validator script and that was all fine before because the design was different before I took over.However having reached this stage I realised that with so many elements that can potentially be on the page, I didn't want to define every possible field.Like so far I've set the maximum file upload to 12, and if I write out all the possible generated fields in the script I'd end up with probably +100 lines of code just for the sake of validating.So I was thinking of possibly sorting these form elements IDs into arrays and link them to the validation parts.So my idea is something likevar myTitles=new Array();myTitles[0]="singletitle1";myTitles[1]="singletitle2";myTitles[2]="singletitle3";var myyear=new Array();myTitles[0]="1998";etc...And then linking the validation parts with myTitles because all the IDs are stored in there.I was just wondering if this was a good idea and if it's possible because the idea seems to make sense in my strange head.Or if you have a better idea how I can validate these generated elements I'm open to suggestions! Many thanks in advance!Or a smarter thing to do is possibly use loops?For your reference:http://cpulse.dmu.ac.uk/upload/Alternative.../albupload.html

Link to comment
Share on other sites

Since no one's answered, yes, that plan will work. I assume your arrays will be grouped into items that can be validated in the same manner, like email addresses, telephone numbers, filenames, etc.Another option is to group things on the fly according to input names, classNames, fieldsets, and so on. The advantage here is that you can modify your form 6 months from now without having to remember to modify your arrays. This is especially advantageous if you ever add form elements dynamically.(And of course this validation is for the user's convenience. I assume you're validating server-side also.)

Link to comment
Share on other sites

Indeed that's the plan, so I'll have separated arrays that are grouped.I was actually thinking about something similar to your option too this afternoon by grouping with the help of types. The one thing I dont like about arrays is the need to update them should I need to change content.I'll look into that as well, it seems to be a lot more convenient and flexible.And yes server-side validation is also part of the plan, many thanks for clarifying that for me DD. :)

Link to comment
Share on other sites

  • 2 weeks later...

Reviving this thread.http://cpulse.dmu.ac.uk/upload/Alternative.../albupload.htmlI've thought of a nice quick and easy way to validate this form with the help of a javascript form validator (gen_validatorv31.js).I've decided to use a loop to validate those individual parts.

 var frmvalidator  = new Validator("myform");  for (i = 1; i <= 12; i++) <!-- Where    i <=    value maximum is the same as the file upload max. So if 10 file upload maximum at one time   i <= 10 { frmvalidator.addValidation("title","alphanumeric_space");  frmvalidator.addValidation("title","req","Album Title \n\n Please enter a name");  frmvalidator.addValidation("title","maxlen=20","Album Title \n\n 20 characters maximum");   frmvalidator.addValidation("edtitle","alphanumeric_space");  frmvalidator.addValidation("edtitle","req"," Track Title \n\n Please enter a name");  frmvalidator.addValidation("edtitle","maxlen=20","Track Title \n\n 20 characters maximum");  frmvalidator.addValidation("edyear","numeric");  frmvalidator.addValidation("edyear","req"," Year \n\n Please enter a year");  frmvalidator.addValidation("edyear","maxlen=4","Year \n\n 4 numbers maximum");  frmvalidator.addValidation("edyear","minlen=4","Year \n\n 4 numbers minimum");  frmvalidator.addValidation("singletitle" + i,"alphanumeric_space");  frmvalidator.addValidation("singletitle" + i,"req","Album Title \n\n Please enter a name");  frmvalidator.addValidation("singletitle" + i,"maxlen=20","Album Title \n\n 20 characters maximum");  frmvalidator.addValidation("ayear" + i,"numeric");  frmvalidator.addValidation("ayear" + i,"req"," Year \n\n Please enter a year");  frmvalidator.addValidation("ayear" + i,"maxlen=4","Year \n\n 4 numbers maximum");  frmvalidator.addValidation("ayear" + i,"minlen=4","Year \n\n 4 numbers minimum");   frmvalidator.addValidation("term","shouldselchk=on");  frmvalidator.addValidation("term","req","Terms and Conditions \n\n Please tick the checkbox to agree to the Terms and Conditions");   };

That didn't quite work so I also created a function with the same parts, and now it doesn't validate at all.If I load that function into the header it throws some error where it can't find the form, so I've put that in the body with the other code.But I thought in principle this should be pretty simple and I can't see why it's not working.Any thoughts on this? Many thanks in advance!

Link to comment
Share on other sites

Oh great...so it doesn't matter how much sense the JavaScript makes if it doesn't handle it in the first place.No wonder I was so lost...this needs to be in pretty fast, any alternatives you can think of? :)

Link to comment
Share on other sites

Maybe, I don't know if that will work with the form validator you're using though. If it's programmed so that it goes around setting events on all of the fields then that might work, because it would probably overwrite itself every time. But with the hidden fields on your song input form I'm not sure if that it will work so well.What exactly are you trying to accomplish with the validator? It might be easier to just do it manually.

Link to comment
Share on other sites

Oh I just want it to validate the required fields that's all.But of course other than the album title and the need for at least 1 track, the generated form elements need validating as neccessary.That's what I'm finding it doesn't really find the fields I want on the first click, if I backup into the page again it might pick it up again but of course I don't want that.But what I really need with the validator is obvious things like text format like alphabet, alphanumerics, numerics as relevant but for the required fields as generated.The one that's always fixed is of course the album title itself, then the rest at the moment is track title, year of album and a genre (where genre you either have one from the drop down or you select other and fill in the field).

Link to comment
Share on other sites

It's probably going to be easiest to do validation manually for the popup box. You can have some code run when the save button is pressed to check each of those fields before adding them to the rest of the form. For the fields that don't change you can either use the existing form validator or do the same type of manual validation for those. It looks like the validator you're using assigns an onsubmit handler to the form so it validates everything when you submit the form. Since you're using the popup box with the fields those fields won't exist when the form submits (and you probably want to validate them before submitting the form anyway), so you need to validate those each time the save button is pressed.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...