am writing this sample script for my simple project. I have all text fields only. However my script perfectly validating only first field, even I wrote same validation script for remaining fields.
For the 2nd field it's not checking for Alphabets and from there onwards it's not checking any validation. ( I have upto 10 fields in my original Script). Pls suggest. I am very desperate for this.
// check to see if the field is blank
if (theForm.sname.value == "")
{
alert("Sandbox name should not be blank");
theForm.sname.focus();
return (false);
}
// require at least 3 characters be entered
if (theForm.sname.value.length < 3)
{
alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}
// allow ONLY Capital Alphabeticals
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
var checkStr = theForm.sname.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only Capital Alphabetics \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}
if (theForm.plead.value == "")
{
alert("Project Lead login field should not be blank");
theForm.plead.focus();
return (false);
}
var checkOk1= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var checkStr1 = theForm.plead.value;
var allValid1= true;
for (i = 0; i < checkStr1.length; i++)
{
ch = checkStr1.charAt(i);
for (j = 0; j < checkOK1.length; j++)
if (ch == checkOK1.charAt(j))
break;
if (j == checkOK1.length)
{
allValid1 = false;
break;
}
}
if (!allValid1)
{
alert("Please enter only Alphabets \"Project Lead Login\" field.");
theForm.sname.focus();
return (false);
}
if (theForm.talias.value == "")
{
alert("Team alias should not be blank");
theForm.talias.focus();
return (false);
}
var checkOk2= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
var checkStr2 = theForm.talias.value;
var allValid2= true;
for (i = 0; i < checkStr2.length; i++)
{
ch = checkStr2.charAt(i);
for (j = 0; j < checkOK2.length; j++)
if (ch == checkOK2.charAt(j))
break;
if (j == checkOK2.length)
{
allValid2 = false;
break;
}
}
if (!allValid2)
{
alert("Please enter only Alphabets \"Team Alias\" field.");
theForm.talias.focus();
return (false);
}
return(true);
}
<!DOCTYPE html><html><body><h3>Send review request to test@something.com</h3><form action="mailto:ynaveen9@something.com" method="post" name="theForm" onsubmit="return FormValidator(this);" >
Sandbox Name: <br/><input name="sname" /><br/>
Project Lead: <br/><input name="plead" /><br/>
TeamAlias: <br/><input name="talias" /><br/><input type="submit" value="Send Mail" name="btn"><input type="reset" value="Reset"></form><script>
// The form validating script should always be placed after the form!
function FormValidator(theForm) {
// check to see if the field is blank
if (theForm.sname.value == "") {
alert("Sandbox name should not be blank!");
theForm.sname.focus();
return(false);
}
// require at least 3 characters to be entered
if (theForm.sname.value.length < 3) {
alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}
// allow ONLY Capital Alphabets
if (!(/^[A-Z ]*$/.test(theForm.sname.value))) { // using "regex" in javascript. Check: "http://stackoverflow.com/questions/8411809/regular-expression-for-upper-case-letter-only-in-javascript"
alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
theForm.sname.focus();
return(false);
}
/*var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
var checkStr = theForm.sname.value;
var allValid = true;
for (i=0; i<checkStr.length; i++) {
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++) {
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length) {
allValid = false;
break;
}
}
}
if (!allValid) {
alert("Please enter only Capital Alphabetics \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}*/
if (theForm.plead.value == "") {
alert("\"Project Lead\" login field should not be blank!");
theForm.plead.focus();
return(false);
}
if (!(/^[A-Za-z]*$/.test(theForm.plead.value))) {
alert("Please enter only Alphabets in the \"Project Lead\" login field.");
theForm.plead.focus();
return(false);
}
/*var checkOk1= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var checkStr1 = theForm.plead.value;
var allValid1= true;
for (i = 0; i < checkStr1.length; i++) {
ch = checkStr1.charAt(i);
for (j = 0; j < checkOK1.length; j++) {
if (ch == checkOK1.charAt(j))
break;
if (j == checkOK1.length) {
allValid1 = false;
break;
}
}
}
if (!allValid1) {
alert("Please enter only Alphabets \"Project Lead Login\" field.");
theForm.sname.focus();
return (false);
}*/
if (theForm.talias.value == "") {
alert("\"TeamAlias\" field should not be blank!");
theForm.talias.focus();
return(false);
}
if (!(/^[A-Za-z]*$/.test(theForm.talias.value))) {
alert("Please enter only Alphabets in the \"TeamAlias\" field.");
theForm.talias.focus();
return(false);
}
/*var checkOk2= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
var checkStr2 = theForm.talias.value;
var allValid2= true;
for (i = 0; i < checkStr2.length; i++) {
ch = checkStr2.charAt(i);
for (j = 0; j < checkOK2.length; j++) {
if (ch == checkOK2.charAt(j))
break;
if (j == checkOK2.length) {
allValid2 = false;
break;
}
}
}
if (!allValid2) {
alert("Please enter only Alphabets \"Team Alias\" field.");
theForm.talias.focus();
return (false);
}*/
return(true); // returning "true" after validating all the form fields to submit the form!
}
</script></body></html>
Come back to me if you do NOT understand a thing!
Also, do NOT forget to compare my code with yours to find the errors, to know where your code was outdated etc.
"It will never rain roses: when we want to have more roses, we must plant more roses."
- George Eliot
// check to see if the field is blank
if (theForm.sname.value == "" )
{
alert("Sandboxname is mandatory");
return (false);
}
// require at least 3 characters be entered
else if (theForm.sname.value.length < 3)
{
alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}
// allow ONLY Capital Alphabets
if (!(/^[A-Z ]*$/.test(theForm.sname.value)))
{
alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
theForm.sname.focus();
return(false);
}
if (theForm.talias.value == "" )
{
alert("TeamAlias is mandatory");
return (false);
}
if (!(/^[A-Za-z-]*$/.test(theForm.talias.value)))
{
alert("Please enter only Alphabets in the \"Team alias Name\" field!");
theForm.talias.focus();
return(false);
}
if (theForm.plead.value == "" )
{
alert("Project Lead is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.plead.value)))
{
alert("Please enter only Alphabets in the \"Project Lead Name\" field!");
theForm.plead.focus();
return(false);
}
if (theForm.category.value == "" )
{
alert("Category is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.category.value)))
{
alert("Please enter only Alphabets in the \"Category \" field!");
theForm.category.focus();
return(false);
}
if (theForm.type.value == "" )
{
alert("type is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.type.value)))
{
alert("Please enter only Alphabets in the \"type \" field!");
theForm.type.focus();
return(false);
}
if (theForm.item.value == "" )
{
alert("item is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.item.value)))
{
alert("Please enter only Alphabets in the \"item \" field!");
theForm.item.focus();
return(false);
}
if (theForm.quota.value == "" )
{
alert("quota is mandatory");
return (false);
}
if (!(/^[0-9]*$/.test(theForm.quota.value)))
{
alert("Please enter only Digits in the \"quota \" field!");
theForm.quota.focus();
return(false);
}
if (theForm.scname.value == "" )
{
alert("Schema name is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.scname.value)))
{
alert("Please enter only Alphabets in the \"Schema name \" field!");
theForm.scname.focus();
return(false);
}
if (theForm.tt.value == "" )
{
alert("TT ref. is mandatory");
return (false);
}
if (!(/^[0-9]*$/.test(theForm.tt.value)))
{
alert("Please enter only Digits in the \"TT ref. \" field!");
theForm.tt.focus();
return(false);
}
if (theForm.ldap.value == "" )
{
alert("LDAP name is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.ldap.value)))
{
alert("Please enter only Alphabets in the \"LDAP name \" field!");
theForm.ldap.focus();
return(false);
}
return (true);
}
</script>
</body>
</html>
This is my entire code which is again failing to validate. I just used same simple Regex type validation for all the fields.. It's bit time consuming to go through this.. But pls do for me..
// check to see if the field is blank
if (theForm.sname.value == "" )
{
alert("Sandboxname is mandatory");
return (false);
}
// require at least 3 characters be entered
else if (theForm.sname.value.length < 3)
{
alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
theForm.sname.focus();
return (false);
}
// allow ONLY Capital Alphabets
if (!(/^[A-Z ]*$/.test(theForm.sname.value)))
{
alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
theForm.sname.focus();
return(false);
}
if (theForm.talias.value == "" )
{
alert("TeamAlias is mandatory");
return (false);
}
if (!(/^[A-Za-z-]*$/.test(theForm.talias.value)))
{
alert("Please enter only Alphabets in the \"Team alias Name\" field!");
theForm.talias.focus();
return(false);
}
if (theForm.plead.value == "" )
{
alert("Project Lead is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.plead.value)))
{
alert("Please enter only Alphabets in the \"Project Lead Name\" field!");
theForm.plead.focus();
return(false);
}
if (theForm.category.value == "" )
{
alert("Category is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.category.value)))
{
alert("Please enter only Alphabets in the \"Category \" field!");
theForm.category.focus();
return(false);
}
if (theForm.type.value == "" )
{
alert("type is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.type.value)))
{
alert("Please enter only Alphabets in the \"type \" field!");
theForm.type.focus();
return(false);
}
if (theForm.item.value == "" )
{
alert("item is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.item.value)))
{
alert("Please enter only Alphabets in the \"item \" field!");
theForm.item.focus();
return(false);
}
if (theForm.quota.value == "" )
{
alert("quota is mandatory");
return (false);
}
if (!(/^[0-9]*$/.test(theForm.quota.value)))
{
alert("Please enter only Digits in the \"quota \" field!");
theForm.quota.focus();
return(false);
}
if (theForm.scname.value == "" )
{
alert("Schema name is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.scname.value)))
{
alert("Please enter only Alphabets in the \"Schema name \" field!");
theForm.scname.focus();
return(false);
}
if (theForm.tt.value == "" )
{
alert("TT ref. is mandatory");
return (false);
}
if (!(/^[0-9]*$/.test(theForm.tt.value)))
{
alert("Please enter only Digits in the \"TT ref. \" field!");
theForm.tt.focus();
return(false);
}
if (theForm.ldap.value == "" )
{
alert("LDAP name is mandatory");
return (false);
}
if (!(/^[A-Za-z]*$/.test(theForm.ldap.value)))
{
alert("Please enter only Alphabets in the \"LDAP name \" field!");
theForm.ldap.focus();
return(false);
}
Bookmarks