shivabharat Posted June 2, 2003 Share Posted June 2, 2003 Hi this a Java script code generator . To use this paste the code in the HTML file for which u need a validation script. Put this script at the end of the page and browse the page in the browser. You will see the code shown in the HTML page. Copy the code and use it. :wink: Hope this helps!! <SCRIPT LANGUAGE=javascript> <!-- document.write("function trim(strField)" + "<br>"); document.write("{" + "<br>"); document.write(" for(loop=0,space=0;strField.substr(loop,1)==" " &&loop < strField.length;loop++,space++);" + "<br>"); document.write(" strField=strField.substr(space,(strField.length-space));" + "<br>"); document.write(" for(loop=strField.length,space=0;strField.substr(loop-1,1)==" " && loop>0;loop--,space++);" + "<br>"); document.write(" strField=strField.substr(0,strField.length-space);" + "<br>"); document.write(" return strField;" + "<br>"); document.write("}" + "<br>"); document.write("function validateForm()" + "<br>"); document.write("{" + "<br>"); for(var i=0;i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type=="text" |document.forms[0].elements[i].type=="textarea") { document.write("document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value=trim(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value);<br>"); } } for(var i=0;i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type=="select-one") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".selectedIndex==0)<br>"); document.write("{<br>"); document.write(" alert("Please select a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="text") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value=="")<br>"); document.write("{<br>"); document.write(" alert("Please enter a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="password") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value=="")<br>"); document.write("{<br>"); document.write(" alert("Please enter a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="checkbox") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".checked==false)<br>"); document.write("{<br>"); document.write(" alert("Please check a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="radio") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".status==false)<br>"); document.write("{<br>"); document.write(" alert("Please check a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="textarea") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value=="")<br>"); document.write("{<br>"); document.write(" alert("Please enter a value");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } } document.write("return true;" + "<br>"); document.write("}" + "<br>"); //--> </SCRIPT> Link to comment Share on other sites More sharing options...
Dr Bloke Posted February 18, 2004 Share Posted February 18, 2004 The code has several unescaped quotes. I believe it should look like this: <script LANGUAGE=javascript> <!-- document.write("function trim(strField)" + "<br>"); document.write("{" + "<br>"); document.write(" for(loop=0,space=0;strField.substr(loop,1)==\" \" &&loop < strField.length;loop++,space++);" + "<br>"); document.write(" strField=strField.substr(space,(strField.length-space));" + "<br>"); document.write(" for(loop=strField.length,space=0;strField.substr(loop-1,1)==\" \" && loop>0;loop--,space++);" + "<br>"); document.write(" strField=strField.substr(0,strField.length-space);" + "<br>"); document.write(" return strField;" + "<br>"); document.write("}" + "<br>"); document.write("function validateForm()" + "<br>"); document.write("{" + "<br>"); for(var i=0;i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type=="text" |document.forms[0].elements[i].type=="textarea") { document.write("document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value=trim(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value);<br>"); } } for(var i=0;i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type=="select-one") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".selectedIndex==0)<br>"); document.write("{<br>"); document.write(" alert(\"Please select a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="text") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value==\"\")<br>"); document.write("{<br>"); document.write(" alert(\"Please enter a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="password") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value==\"\")<br>"); document.write("{<br>"); document.write(" alert(\"Please enter a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="checkbox") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".checked==false)<br>"); document.write("{<br>"); document.write(" alert(\"Please check a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="radio") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".status==false)<br>"); document.write("{<br>"); document.write(" alert(\"Please check a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } if(document.forms[0].elements[i].type=="textarea") { document.write("if(document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".value==\"\")<br>"); document.write("{<br>"); document.write(" alert(\"Please enter a value\");<br>"); document.write(" document."+document.forms[0].name+"."+document.forms[0].elements[i].name+".focus();<br>"); document.write(" return false;<br>"); document.write("}<br>"); } } document.write("return true;" + "<br>"); document.write("}" + "<br>"); //--> </SCRIPT> Link to comment Share on other sites More sharing options...
PHP Monkeh Posted July 4, 2004 Share Posted July 4, 2004 I pasted this code into a file and tried it out. What exactly is it supposed to do? All it does is echo the code you've told it to... don't see a point to the whole thing yet, anyone want to explain? Link to comment Share on other sites More sharing options...
Recommended Posts