0
<!DOCTYPE html>
<html>
<body>    

<h3> Send review request to  test@something.com:</h3>

<script  type="text/javascript">

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 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);
}

</script>

<form action="MailTo:ynaveen9@something.com" method="post" name="theForm" onsubmit="return FormValidator(this);" >

<p>
Sandbox Name:<br />
<input type="text" name="sname" /><br />
Project Lead<br />
<input type="text" name="plead" /><br />
TeamAlias<br />
<input type="text" name="talias" /><br />

<input type="Submit" value="Sendmail" name="btn">
<input type="reset" value="Reset"> </p>
</form>
</body>
</html>

I have one script which irritating me for some days. This is perfectly validating 1st field. For the 2nd field it's not validating for alphabets and there onwards it's not checking any Validation. I have 10 fields in my original script, all text fields only). This is really frustrrating me. Could u pls help?

Am using same validation script for all the fields. Not sure why it's not working..

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Taywin
0

Your alphabet validation is wrong and that will always return false. The reason is that line 39 and so on will be guaranteed that the j!=checkOK.length because the lenght of checkOK is always 27 and it will be true if and only if your character is '_' (an other checks have similar error but not exactly the same character & length).

A simplest way to validate character is to use regular expression. The different set of regular expression will be used depending on what you want to check. Look at the sample below which works similarly to lines 20~44.

// lines 20~44
var checkStr = theForm.sname.value
if (checkStr.length<3)
  alert("The length of name must be at least 3")
  theForm.sname.focus()
  return false
}
else if (!checkStr.match(/^[A-Z_]+$/)) {  // match A-Z and _ from start to end
  alert("Please enter only Capital Alphabetics \"Sandbox Name\" field.")
  theForm.sname.focus()
  return false
}

// for other portion, you replace the "theForm.sname.value" with something else,
// and you need to replace the regular expression to match what you want.
// Please note that you should declare variables locally using 'var' keyword.
// Also, you do not need to declare a new variable but reuse the 'checkStr',
// but you do not redeclare the variable again (using 'var').
// See example below for the 2nd check.
checkStr = theForm.plead.value
if (checkStr.length<1)
  alert("The length of name must be blank")
  theForm.plead.focus()
  return false
}
else if (!checkStr.match(/^[A-Za-z]+$/)) {  // match either A-Z or a-z from start to end
  alert("Please enter only Alphabets \"Project Lead Login\" field.")
  theForm.plead.focus()
  return false
}

// regular expression to match line 88 ==> /^[A-Za-z-]+$/

// More notes:
// When you want to check for empty, do NOT use value=="" because it will fail
// if the value contains all white space characters. Either trim it and check
// or use a regular expression.
// i.e.
var value = "  "
if (value=="") { alert("Empty!") }  // FAIL
else { alert("Has something") }  // display this

// one way to do is to trim it first
if (value.replace(/^\s+|\s+$/g,"")=="") { alert("Empty") }  // SUCCESS
else { alert("Has something") }

Hope this help you to understand JavaScript a bit more.

Edited by Taywin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.