| | |
Need help with Javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Method 1: You can change the form submit field into a button field and attach the validation to the onclick event as mentioned before.
Take:
[HTML]<input type='submit' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" />[/HTML]
And change it to:
[HTML]<input type='button' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" onclick="vaildate_user_text();" />[/HTML]
In the function vaildate_user_text() you'll have to make sure the objects references to the HTML form elements are correctly referencing your form and fields.
ie:
[HTML]var compare_text=document.form1.user_text.value;[/HTML]
Should point to the form field of you are vaildating..
[HTML]document.form1.submit();[/HTML]
Should correctly point to the form to submit.
Method 2: Or you can attach a listerner to the form submit button, this way you don't have to change the submit field to a button field, so that it works with browsers not supporting JS.
In your code the form is not showing, but this is what it would look like:
<form name="you_form_name" onsubmit="return vaildate_user_text();">
Notice the "return vaildate_user_text();" and not just "vaildate_user_text()". The reason for this is that the form will be submitted if the return from the onsubmit event is === true (or NULL) and will not sumit if the return is === false.
Then in validate_user_text(), you'll have to make sure the object references to the form elements are correct, just like in Method 1.
The only other change is that you will not need the:
[HTML]document.form1.submit();[/HTML]
In place of it put:
[HTML]return true;[/HTML]
This return is returned to the form submit event, and will continue submitting the form.
After:
[HTML]
alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
document.form1.user_text.select();[/HTML]
Add:
[HTML]return false;[/HTML]
This will return false to the form.submit() event and thus cancel the form submission.
Hope that gets you moving forward...
Take:
[HTML]<input type='submit' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" />[/HTML]
And change it to:
[HTML]<input type='button' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" onclick="vaildate_user_text();" />[/HTML]
In the function vaildate_user_text() you'll have to make sure the objects references to the HTML form elements are correctly referencing your form and fields.
ie:
[HTML]var compare_text=document.form1.user_text.value;[/HTML]
Should point to the form field of you are vaildating..
[HTML]document.form1.submit();[/HTML]
Should correctly point to the form to submit.
Method 2: Or you can attach a listerner to the form submit button, this way you don't have to change the submit field to a button field, so that it works with browsers not supporting JS.
In your code the form is not showing, but this is what it would look like:
<form name="you_form_name" onsubmit="return vaildate_user_text();">
Notice the "return vaildate_user_text();" and not just "vaildate_user_text()". The reason for this is that the form will be submitted if the return from the onsubmit event is === true (or NULL) and will not sumit if the return is === false.
Then in validate_user_text(), you'll have to make sure the object references to the form elements are correct, just like in Method 1.
The only other change is that you will not need the:
[HTML]document.form1.submit();[/HTML]
In place of it put:
[HTML]return true;[/HTML]
This return is returned to the form submit event, and will continue submitting the form.
After:
[HTML]
alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
document.form1.user_text.select();[/HTML]
Add:
[HTML]return false;[/HTML]
This will return false to the form.submit() event and thus cancel the form submission.
Hope that gets you moving forward...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
OK I tried both methods! With this first method, the submit did nothing at all.
With the second method I tried, the form still submitted anyway
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript1.2"> var swear_words_arr=new Array("silly","bloody","idiot","slut"); var swear_alert_arr=new Array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.REPLIER.submit(); for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } if(swear_alert_count>0) { alert("The Post cannot be submitted.\nThe following Ugly words were found:\n_______________________________\n" + alert_text + "\n_______________________________"); document.REPLIER.user_text.select(); } else { document.REPLIER.submit(); } } function select_area() { document.REPLIER.user_text.select(); } window.onload=reset_alert_count; </script> USING... <input type='button' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" onclick="vaildate_user_text();" />
With the second method I tried, the form still submitted anyway
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript1.2"> var swear_words_arr=new Array("silly","bloody","idiot","*******"); var swear_alert_arr=new Array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.REPLIER.user_text.value; for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } if(swear_alert_count>0) { alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________"); document.REPLIER.user_text.select(); } else { document.REPLIER.submit(); } } function select_area() { document.REPLIER.user_text.select(); } window.onload=reset_alert_count; </script> WITH FORM ALTERED TO... <form name='REPLIER' action="{ibf.script_url}" method='post' onsubmit='return vaildate_user_text()' enctype='multipart/form-data'> THIS IS THE ORIGINAL <form name='REPLIER' action="{ibf.script_url}" method='post' onSubmit='return ValidateForm()' enctype='multipart/form-data'>
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
W. C. Fields
•
•
•
•
OK I tried both methods! With this first method, the submit did nothing at all.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript1.2"> var swear_words_arr=new Array("silly","bloody","idiot","slut"); var swear_alert_arr=new Array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.REPLIER.submit(); for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } if(swear_alert_count>0) { alert("The Post cannot be submitted.\nThe following Ugly words were found:\n_______________________________\n" + alert_text + "\n_______________________________"); document.REPLIER.user_text.select(); } else { document.REPLIER.submit(); } } function select_area() { document.REPLIER.user_text.select(); } window.onload=reset_alert_count; </script> USING... <input type='button' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" onclick="vaildate_user_text();" />
With the second method I tried, the form still submitted anyway
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript1.2"> var swear_words_arr=new Array("silly","bloody","idiot","*******"); var swear_alert_arr=new Array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.REPLIER.user_text.value; for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } if(swear_alert_count>0) { alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________"); document.REPLIER.user_text.select(); } else { document.REPLIER.submit(); } } function select_area() { document.REPLIER.user_text.select(); } window.onload=reset_alert_count; </script> WITH FORM ALTERED TO... <form name='REPLIER' action="{ibf.script_url}" method='post' onsubmit='return vaildate_user_text()' enctype='multipart/form-data'> THIS IS THE ORIGINAL <form name='REPLIER' action="{ibf.script_url}" method='post' onSubmit='return ValidateForm()' enctype='multipart/form-data'>
There are 3 things I can see that can cause problems.
1) First Prob:
In the first method, the submit does nothing. This is because you have the input button named "submit".
[HTML]<input type='button' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" onclick="vaildate_user_text();" /> [/HTML]
This overwrites the submit method of the form replacing it with the form input element. What you can do is name the form input "_submit" or something like that, it usually would not matter, unless the server side code checks for its existence (which you can also change server side).
2) Second Prob:
In method 2 you have:
[HTML] if(swear_alert_count>0)
{
alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
document.REPLIER.user_text.select();
}
else
{
document.REPLIER.submit();
}[/HTML]
This should be:
[HTML] if(swear_alert_count>0)
{
alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
document.REPLIER.user_text.select();
return false;
}
else
{
return true;
}[/HTML]
With method 2, you have to return either boolean TRUE or FALSE. The onsubmit() form event will carry on if you return TRUE, and thus submit for the form, or stop if you return FALSE.
I'd suggest you go with method 2. Therefore you will not need to rename the submit button, and you can control the form submission just by returning true or false.
3) Third Prob:
In method 2, the original form open tag looked like:
[HTML]<form name='REPLIER' action="{ibf.script_url}" method='post' onSubmit='return ValidateForm()' enctype='multipart/form-data'>[/HTML]
As you can see, there is already a form validation there. So if you replace it with your validation, you are bypassing the validation that was there before.
What you should do is create a new function for validation, as such:
[HTML]
function myValidateForm() {
if (!ValidateForm()) {
return false;
}
return validate_user_text();
}
[/HTML]
This new function will first check if the ValidateForm() returns FALSE. If it does, it will return false.
If ValidateForm() returns TRUE, it will return validate_user_text()'s return.
This way both functions have to return true for the form to be submitted.
You'll have to change the form opening tag to:
[HTML]
<form name='REPLIER' action="{ibf.script_url}" method='post' onSubmit='return myValidateForm()' enctype='multipart/form-data'>[/HTML]
--
A note on debugging your JS.
Since the form submit will reload the page, and you wont be able to see any errors, I suggest you put alerts in different parts of the code to see whats happening...
You can also download firebug. http://www.getfirebug.com/
Its has an awesome JS debugging platform (best In my book). You can go through the JS line by line, so see whats happening...
:cheesy:
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
forgot to mention.
Firebug is a firefox extension. You'll need to have firefox (I believe 2.0) in order to use it.
For JS debugging, firefox give's better error codes anyways, IE error codes are very limited. Plus, firefox is used by almost 50% or so users nowdays, so you'll need to debug in firefox anyways..
Firebug is a firefox extension. You'll need to have firefox (I believe 2.0) in order to use it.
For JS debugging, firefox give's better error codes anyways, IE error codes are very limited. Plus, firefox is used by almost 50% or so users nowdays, so you'll need to debug in firefox anyways..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Wow Your Patient!
OK tried this, but form still submitted, do I need to change the submit button part?
OK tried this, but form still submitted, do I need to change the submit button part? JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script language="JavaScript1.2"> var swear_words_arr=new Array("silly","bloody","idiot","bugger"); var swear_alert_arr=new Array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.REPLIER.user_text.value; for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } function myValidateForm() { if (!ValidateForm()) { return false; } return validate_user_text(); } if(swear_alert_count>0) { alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________"); document.REPLIER.user_text.select(); return false; } else { return true; } } function select_area() { document.REPLIER.user_text.select(); } window.onload=reset_alert_count; </script> USING <form name='REPLIER' action="{ibf.script_url}" method='post' onSubmit='return myValidateForm()' enctype='multipart/form-data'>
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
W. C. Fields
![]() |
Similar Threads
- Javascript links not working (Web Browsers)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
- recommendations for JavaScript learning resources? (JavaScript / DHTML / AJAX)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Javascript Useful? (IT Professionals' Lounge)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Reply Back?
- Next Thread: button Swapping
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column cookies createrange() css cursor decimal dependent design disablefirebug dom download dropdown editor element engine error events explorer ext file form forms google gwt gxt highlightedword html htmlform ie8 iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jump listbox math microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem progressbar rated rating regex runtime scroll search select session shopping size sql star starrating stars synchronous text textarea validation w3c web website window windowofwords windowsxp wysiwyg xml \n






