Jump to content

Java script Code Generator


shivabharat

Recommended Posts

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

  • 8 months later...

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

  • 4 months later...
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.