Using JavaScript validation

Shows some Tutorials

Using JavaScript validation

Postby GreyHead on Fri Jul 20, 2007 3:32 pm

Hi,

Caution: I'm no JavaScript expert so there may be errors in this post. If you find any let me know and I'll fix them.

ChronoForms makes it easy to include validation JavaScript with your form but there are some problems in using JavaScript from other sources or existing forms.

Mostly there are two problems:
  • ChronoForms changes the form name and the JavaScript doesn't recognise it.
  • How to call the JavaScript validation - what code to put where.
I'll describe here a basic method that should fix most of these problems. There are other methods that may work as well or better for you.

The form name

ChronoForms changes the form name you use in the Form Manager. If you enter 'my_form' the ChronoForms form will be called 'ChronoContact_my_form'. This is probably the biggest cause of JavaScript validation problems.

Validation scripts usually start with a line like 'function checkForm()' or 'function checkForm(form)' and these two handle the form name in different ways.

Style 1: 'function checkForm()'

The form is usually addressed with the code 'document.{form_name}' e.g. 'document.my_form'

To use this style with ChronoForms you would need to find and replace each 'document.my_form' with 'document.ChronoContact_my_form'. This is tedious and has to be repeated if you want to use the same validation with another form.

Instead I suggest that you change your JavaScript to the second style of function.

Style 2: 'function checkForm(form)'

This style of function uses the JavaScript variable 'form' to identify the form and the actual form name is passed to the function when it is called.

If your JavaScript has the other style, then change the function header to the 'checkForm(form)' style and replace every occurrence of 'document.{form_name}' with 'form' so that 'document.my_form.some_code' becomes 'form.some_code' etc.

Calling the JavaScript

Once you have edited the JavaScript to use the 'checkForm(form)' you can call it simply by adding this code into the ChronoForms 'Form tag attachment' field:
Code: Select all
onSubmit="return checkForm(this)"
Using 'this' here will mean that the correct form name is automatically passed to the JavaScript.

The 'return' means that the form will only be submitted if the JavaScript validation returns 'true' - this is how validation scripts are usually written so is unlikely to be a problem.

Here is a very simple example of a validation script written like this, it simply validates that the form field 'name' is not empty :
Code: Select all
function checkForm(form)
{
if(form.name.value == ""«»)  {
   alert("Please enter your name!"«»);
   return false;
  }
return true;
}


Bob<br><br>Post edited by: GreyHead, at: 2007/07/20 11:34
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3417
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Using JavaScript validation

Postby goranbaxy on Fri Oct 26, 2007 6:41 pm

Here is my JS code:
Some text is on Croatian :(
but if I catch some time I'll translate it!
Code: Select all
function validate_email(field,alerttxt)
{
   with (field)
   {
      apos=value.indexOf("@"«»)
      dotpos=value.lastIndexOf("."«»)
      if (apos<1||dotpos-apos<2)
        {alert(alerttxt);return false}
   }
}

function checkForm(form)
{
   /* IME */        
    if(form.name.value == ""«»)  {
      alert("Niste unjeli ime!"«»);
      return false;
     }
   /* PREZIME */    
    if(form.subname.value == ""«»)  {
      alert("Niste unjeli prezime!"«»);
      return false;
     }
   /* MAIL */    
   with (form)
   {
   if (validate_email(email,"Niste unjeli ispravnu mail adresu!"«»)==false)
     {email.focus();return false}
   } 
   /* TELEFON */
    if(form.tel.value == ""«»)  {
      alert("Niste unjeli broj telefona!"«»);
      return false;
     }   
   /* ADRESA */
    if(form.adresa.value == ""«»)  {
      alert("Niste unjeli adresu!"«»);
      return false;
     }   
   /* KU&#262;NI BROJ */
    if(form.kbroj.value == ""«»)  {
      alert("Niste unjeli ku&#263;ni broj!"«»);
      return false;
     }      
   /* POÅ TANSKI BROJ */
    if(form.posta.value == ""«»)  {
      alert("Niste unjeli poÅ¡tanski broj!"«»);
      return false;
     }      
   /* GRAD */
    if(form.grad.value == ""«»)  {
      alert("Niste unjeli grad!"«»);
      return false;
     }      
   /* ŽUPANIJA */
    if(form.zupanija.value == "21"«»)  {
      alert("Niste izabrali županiju!"«»);
      return false;
     }       
     /* Ovo se izvrÅ¡ava nakon svih provjera! */
     return true;
}
goranbaxy
Fresh Boarder
 
Posts: 13
Joined: Wed Oct 17, 2007 11:55 am

Re:Using JavaScript validation

Postby admin on Fri Oct 26, 2007 7:02 pm

hi goranbaxy,

do you put the checkform(this) in the right field in Chronoforms? is your form online somewhere ?

Max<br><br>Post edited by: admin, at: 2007/10/26 15:02
ChronoForms/ChronoConnectivity/ChronoComments Developer Thanks for using our components!
If you have any problems with any extension please tell us.
If you like any of our extensions please post a review at Joomla.org
Want users to submit their content to your website ? try Submit Story
Want to list/edit/delete your data ? try ChronoConnectivity
Want to have stylish AJAX comments ? try ChronoComments
User avatar
admin
Platinum Boarder
 
Posts: 3181
Joined: Mon Aug 14, 2006 5:29 am

Re:Using JavaScript validation

Postby goranbaxy on Fri Oct 26, 2007 7:07 pm

In "Form tag attachment:" I have: "enctype='multipart/form-data' onSubmit="return checkForm(this)""
everthing is working but it's still not online as it needs some design.
I have put the the html + js code in " Share your forms with others"
goranbaxy
Fresh Boarder
 
Posts: 13
Joined: Wed Oct 17, 2007 11:55 am

Re:Using JavaScript validation

Postby admin on Fri Oct 26, 2007 7:30 pm

Conversation moved here :<br><br>Post edited by: GreyHead, at: 2007/11/06 15:58
ChronoForms/ChronoConnectivity/ChronoComments Developer Thanks for using our components!
If you have any problems with any extension please tell us.
If you like any of our extensions please post a review at Joomla.org
Want users to submit their content to your website ? try Submit Story
Want to list/edit/delete your data ? try ChronoConnectivity
Want to have stylish AJAX comments ? try ChronoComments
User avatar
admin
Platinum Boarder
 
Posts: 3181
Joined: Mon Aug 14, 2006 5:29 am

html tidy

Postby sailsmart on Fri May 16, 2008 11:40 am

On the validating a form thread, can I just press the validate at the bottom of the live form which redirects to a website which shows all line errors, then the choice to use html tidy to fix. This usually helps when using an alternate editor like Dreamweaver. I think I still then have to take out some of the head tags and form tags. Does anyone else do this?
sailsmart
Junior Boarder
 
Posts: 21
Joined: Tue Apr 22, 2008 12:03 pm

Re:Using JavaScript validation

Postby a6april on Fri May 16, 2008 11:07 pm

How do I add more fields to this code???
Code: Select all
function checkForm(form)
{
if(form.Name.value == "")  {
   alert("Please Enter Your Name!");
   return false;
  }
return true;
}
a6april
Junior Boarder
 
Posts: 21
Joined: Sat Feb 23, 2008 8:30 pm

Re:html tidy

Postby GreyHead on Sat May 17, 2008 9:42 am

Hi sailsmart,

I'm not completely sure what you are asking here.

In form validation we are usually talking about using some JavaScript or PHP to check that the form user has made the right kind of entry in a form field (for example, an email address is actually an email address and not a nonsense string).

You can use HTML Tidy to check that your form code is using valid HTML - that's also useful, you can run the page url through one of the on-line validators. I'd recommend that you use FireFox with the FireBug extension which is by far and away the best web-page debugger. (There's also an HTML validator extension for FireFox.)

Bob
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3417
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Using JavaScript validation

Postby GreyHead on Sat May 17, 2008 9:44 am

Hi a6april,

Don't. If you are looking for simple 'required' field validation then use the Validation built in to ChronoForms. Go to the Validation tab turn it ON, set the library to Mootools for Joomla 1.5 (or leave as prototype for Joomla 1.0.x) and put a comma separated list fo field names in the 'required' box.

No more messing with special scripts.

Bob
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3417
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Using JavaScript validation

Postby a6april on Sat May 17, 2008 10:31 am

Thanks Bob,

I had no idea that Validator even worked, I did not know to put it on Mootools, wow that just saved me about 3 or 4 headaches.

I do have a question on how to add the email validation. I scrolled down to the
7- validate-email (a valid email address)

I then put the email field in there, however when I test the form, the email does not get validated and the form gets sent right on through with the email field blank. (I did double check for form field case mis-match.

Also where is the css to change the color of the text that pops up and says required?, can I change that text?
a6april
Junior Boarder
 
Posts: 21
Joined: Sat Feb 23, 2008 8:30 pm

Re:Using JavaScript validation

Postby GreyHead on Sat May 17, 2008 10:39 am

Hi a6april,

You probably need to put the 'email' field in 'required' too. The 'validate-email' field (and the others) work on the basis of 'if there is an entry is it a valid entry' so blank entries are OK. That makes sense when you think about.

The validation works with css classes. Error messages show up with class="validation-advice" so just add some css for that.

Bob
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3417
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Using JavaScript validation

Postby a6april on Sat May 17, 2008 5:37 pm

Are you saying on line 139 of the validation.js file

Code: Select all
advice = '<div class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'" style="display:none">' + errorMsg + '</div>'


I need to edit this, i do not see any css attached to it unless I have to figure out how to say add a color to the code above

or is there a css file somewhere I can't find?
a6april
Junior Boarder
 
Posts: 21
Joined: Sat Feb 23, 2008 8:30 pm

Re:Using JavaScript validation

Postby a6april on Sat May 17, 2008 5:45 pm

AAAHHHHHHH!

Hi Bob, I found it on the mooValidation.js file on line 90 character number: 110 I changed
Code: Select all
setStyle('display','none')

to
Code: Select all
setStyle('color','red')


I am sure there are all sorts of edits you can do there to get creative but I will look into that later, but I hope this helps someone else for now

Thanks Bob for always being there!!:)
a6april
Junior Boarder
 
Posts: 21
Joined: Sat Feb 23, 2008 8:30 pm

Re:Using JavaScript validation

Postby GreyHead on Sat May 17, 2008 5:55 pm

Hi a6april,

If you aren't using any css of your own you can just add a snippet at the beginning of the form html:
Code: Select all
<style type="text/css">
form .validation-advice {color:red;}
</style>
Bob

PS I think that changing display:none to something else may have unexpected side-effects.
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3417
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Using JavaScript validation

Postby a6april on Sat May 17, 2008 6:28 pm

I think your correct, lol I just noticed that the form I am using has a lot of required fields, they are getting cut off after so many characters, could that be a side effect your talking about?

I tried to change the properties of the paramsall in phpMyAdmin for longer text but that is not working..any suggestions?
a6april
Junior Boarder
 
Posts: 21
Joined: Sat Feb 23, 2008 8:30 pm

Next

Return to ChronoForms How To

Who is online

Users browsing this forum: No registered users and 1 guest