JavaScript example - validation

cheesegrits

Support Gopher
I've been noodling around with some JavaScript, thought I'd share this one.

I recently had a powerful need to do JavaScript validation when the Submit button is pressed, aborting the submit if validation of certain fields failed. In this case it's a form that has to create a Joomla user, so I really wanted to validate things like matching password fields, non-blank emails, etc. BEFORE submitting the form. That way I can safely create the user in the "as soon as form submitted" PHP script.

So ... I created a 13.js (the ID of my form) in ./components/com_fabrik/js, which Fabrik will automatically load when the form is displayed, with the following:

Code:
function watchSubmit(e){
    // Select the Submit button and bind our submitMyForm() to the 'click' event.
    // Would need to be a bit more selective about this if more than one form
	var submit_button = $E('input[name=Submit]');
	if (submit_button) {
		submit_button.addEvent('click', function(e){
			e = new Event(e);
			e.stop();
			submitMyform('');
		}.bind(this))
	}
}

function submitMyform() {
    // We get called from the click event handler.
    // Do the form validation, only call form.submit() if it passes all tests.
    // First the regexp we use for validating the characters used in the username
   	var r = new RegExp("[\<|\>|\"|'|\%|\;|\(|\)|\&|\+|\-]", "i");
    // select the form ... it'll always be fabrikXX, where XX is your form ID.
    var form = $E('form[name=fabrik13]')
    // now do all the validations
	if (trim(form.jos_fabrik_formdata_13___name.value) == "") {
		alert( "You must provide a name." );
	} else if (form.jos_fabrik_formdata_13___username.value == "") {
		alert( "You must provide a user login name." );
	} else if (r.exec(form.jos_fabrik_formdata_13___username.value) || form.jos_fabrik_formdata_13___username.value.length < 3) {
		alert( "You login name contains invalid characters or is too short." );
	} else if (trim(form.jos_fabrik_formdata_13___email.value) == "") {
		alert( "You must provide an e-mail address." );
	} else if (form.jos_fabrik_formdata_13___gid.value == "") {
		alert( "You must assign user to a group." );
	} else if (trim(form.jos_fabrik_formdata_13___password.value) != "" && form.jos_fabrik_formdata_13___password.value != form.jos_fabrik_formdata_13___password2.value){
		alert( "Password do not match." );
	} else if (form.jos_fabrik_formdata_13___gid.value == "29") {
		alert( "Please Select another group as `Public Front-end` is not a selectable option" );
	} else if (form.jos_fabrik_formdata_13___gid.value == "30") {
		alert( "Please Select another group as `Public Back-end` is not a selectable option" );
	} else {
        // whoop whoop! we passed all validations, so submit the form.
		form.submit();
	}
    // if we get here, we failed validation, so don't do nuffink
	return false;
}

Window.onDomReady(function(){
    // invoke the watchSubmit function just once when the page loads
    // this will bind our submitMyForm function to the Submit button
	watchSubmit();
})

It'd be easy enough to make this more generic by passing the form ID as an argument, and building the form and element names on the fly. And I'll probably tweak the email test with an actual regexp test for a valid email format. But the above works. Hope it helps someone.

-- hugh
 
Followed your steps but failed to get it working. My test script 11.js with function showbox() { alert("This is a test msg"); } in ./components/com_fabrik/js - where my form number is 11. Then place showbox() into the element javascript with onfocus, onclick, onchange field events. Ran Firebug and confirmed that showbox() function is loaded. However, couldn't find the onfocus, onclick, onchange function calls attached to the text input fields. I thought this is supposed to be quite straight forward. Did I miss something?
 
Did you confirm that those events actually saved on the element?

I found a bug in the element admin that was preventing it from saving any JS with a quote in it.

Should be fixed in the latest SVN rev.

-- hugh
 
I have been going in and out of the element properties page for days to test and the showbox() is still there. I supposed it is saved.
 
Khoon Seng Lim said:
Followed your steps but failed to get it working. My test script 11.js with function showbox() { alert("This is a test msg"); } in ./components/com_fabrik/js - where my form number is 11. Then place showbox() into the element javascript with onfocus, onclick, onchange field events. Ran Firebug and confirmed that showbox() function is loaded. However, couldn't find the onfocus, onclick, onchange function calls attached to the text input fields. I thought this is supposed to be quite straight forward. Did I miss something?

Well you won't see the events as inline code on the elements. It's done by attaching trigger events in the element class on the fly. Read through element.js, you'll see where it happens.

If you want to point me at your page, I'll see if I can see anything. I'm unable to duplicate this problem with the current SVN rev.

-- hugh
 
Hugh, made some progresss. I managed to get the script working with the OnSubmit event handler as per your code. But not when I call the same function from element javascript. I think the element event handler has a problem. Firebug does not show error. If the function can be called from the onsubmit, then why can't it be called from element javascript? Will point you to the pg when I load it up to the internet.
 
Are you using the formbot? If so, I just found a problem that would prevent JS from working properly, they should be fixed in the latest SVN.

Yes, please point me at the page.

-- hugh
 
can't make this code work

Hello
I need to validate a form using javascript so I tried using the code suggested.
I created a 3.js file and uploaded it to /components/fabrik/js.
I'm trying to grab the "click" on the submit button event to check my form but the form keeps sending itself as if there was no script.
I followed the instruction but nothing help.
Is there anything i can do to make it work?

David
 
Try replacing the line:

Code:
 Window.onDomReady(function(){

... with ...

Code:
window.addEvent('domready', function(){

-- hugh
 
Thank you.

Here's the code I'm using:
function watchSubmit(e){
// Select the Submit button and bind our submitMyForm() to the 'click' event.
// Would need to be a bit more selective about this if more than one form
var submit_button = $E('input[name=Submit]');

if (submit_button) {

submit_button.addEvent('click', function(e){

e = new Event(e);
e.stop();
submitMyform('');
}.bind(this))
}
}

function submitMyform() {
alert("hello");
// if we get here, we failed validation, so don't do nuffink
return false;
}

window.addEvent('domready', function(){
// invoke the watchSubmit function just once when the page loads
// this will bind our submitMyForm function to the Submit button
watchSubmit();
})

The watchSubmit function works fine but when I click the Submit button the form is sent anyway.
It seem never getting to the submitMyform function.

Using:
Joomla! 1.0.15
Fabrik 1.0.5.1

David
 
The js page has only the code I sent.
Code:
function watchSubmit(e){
// Select the Submit button and bind our submitMyForm() to the 'click' event.
// Would need to be a bit more selective about this if more than one form
var submit_button = $E('input[name=Submit]');

if (submit_button) {

submit_button.addEvent('click', function(e){

e = new Event(e);
e.stop();
submitMyform('');
}.bind(this))
}
}

function submitMyform() {
alert("hello");
// if we get here, we failed validation, so don't do nuffink
return false;
}

window.addEvent('domready', function(){
// invoke the watchSubmit function just once when the page loads
// this will bind our submitMyForm function to the Submit button
watchSubmit();
})


Do you need to access the actual form?

David
 
Yes, I need a URL to the page so I can debug the JS in action. PM me with it if you don't want to post the URL here.

-- hugh
 
Hello Hugh and David,

I am also facing the same issue, it seems the submitMyform() function is not working for me either, what i was looking to do, is focus then blur on an element after the "Submit" button is pressed (pretty much initiate a calculate total element), jos_fabrik_formdata_4___grandtotal

It does not seem to be working at all, it just submits the form regardless of the additional code in the 4.js file. I also have other functions in the js file, but they should not be interacting with this what so ever, but are working fine.

if i cannot focus one of the forms elements when the form is submitted, i can just write all of the code thats linked to that element in the 4.js file. Either way i cannot do even a simple alert at this stage.

Any help greatly appreciated. Will post if i find out anything.

Josh
 
First thing to try is to replace that first selector line with something more specific, in case it is selecting the wrong Submit button:

$('fabrik13').getElements('input[name=Submit]')[0];

Substitute your form ID and whatever you called your submit button.

Next thing ... you shouldn't have to focus and blur. Just 'functionize' the code you have in the element(s) that you want to run and put it in the form JS. Replace it in the element(s) with a call to the function. And call the same function from the submit watcher.

If it still doesn't work, point me at the page, and PM me the usual details (I still have ftp access, but the J! login you gave me isn't letting me in).

-- hugh
 
Hello,

I use Firefox 3.0.1 and it appears (using Firebug) that "window.addEvent" is not a function. With Internet Explorer 7 I don't see errors but I have the same behavior : when I submit the form, no validation occurs, form is directly submitted.

What should I do ?

Thanks,

Barbara
 
Mine is working without an issue with the latest FF 3.0.3 and firebug installed.also works with Google Crome and IE7, the issue could be unrelated what SVN do you have?
im still on around SVN986ish~~, possibly its an issue with newer builds
 
Yeah, with latest SVN (554) it works fine...

Anyway, it's easier to get form and submit button by document.getElementById :)
 
Hello,

Huu-hoo, with SVN 570 something is broken again for IE7 :'(

Here is where it fails :

Code:
var submit_button = document.getElementById('fabrikSubmit6');
	if (submit_button) {
		[b]submit_button.addEvent('click', function(e){[/b]
			e = new Event(e);
			e.stop();
			submitMyform('');
		}.bind(this))
	}

It tells that this object doesn't deal with this property or method, so I guess submit_button.addEvent goes wrong (using MS script debugger... surprised it seems to work somewhat on IE7 too).

If I do "alert(submit_button)" IE replyes "[object]".

I'm really surprised IE7 complains about this code since it worked at least once...

Well, I'm going to continue this thread in the right part of the forum (Fabrik 2.0 beta testing) with reference to this one.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top