I am trying to write a script that will validate one field in a form (Title). It can not be left blank and it can only be alphanumeric. I am using a javasript at the moment. It has worked before but now if I leave the title field blank it just ignores it. I am also trying to implement a script called StripSymbols to remove any special symbols that the user enters. Well here it is:
<script language="javascript">
function setColor(el, bg)
{
if (el.style) el.style.backgroundColor = bg;
}
function verifyform()
{
var bgBad = "#F7F28A";
if (trim(document.form.Title.value) == "")
{
setColor(document.form.Title, bgBad);
alert("Please enter a title!");
document.form.Title.focus();
return false;
}
if (trim(document.form.EventDate.value) == "")
{
setColor(document.form.EventDate, bgBad);
alert("Please enter a date!");
document.form.EventDate.focus();
return false;
}
if (trim(document.form.Notes.value) == "")
{
setColor(document.form.Notes, bgBad);
alert("Please enter a brief description!");
document.form.Notes.focus();
return false;
}
if (trim(document.form.Description.value) == "")
{
setColor(document.form.Description, bgBad);
alert("Please enter a description!");
document.form.Description.focus();
return false;
}
return true;
}
function trim(strText)
{
while (strText.substring(0,1) == " ")
strText = strText.substring(1, strText.length);
while (strText.substring(strText.length-1,strText.length) == " ")
strText = strText.substring(0, strText.length-1);
return strText;
}
Function StripSymbols(document.form.Title)
{
Dim nCharPos, sOut, nChar
nCharPos = 1
sOut = ""
For nCharPos = 1 To Len(document.form.Title)
nChar = Asc(Lcase(Mid(document.form.Title, nCharPos, 1)))
If ((nChar > 47 And nChar < 58) or_
(nChar > 96 And nChar < 123) or_
nChar = 32) Then
sOut = sOut & Mid(document.form.Title, nCharPos, 1)
End If
Next
StripSymbols = sOut
}
</script>
I am calling the function like this:
<form ENCTYPE="multipart/form-data" action="Manage_Events-A-Step2.asp" name="form" method="post" onsubmit="javascript:return verifyform();">
For some reason it is ignoring the function. As for the StripSymbols function it is really important because the Title can not have any special characters because it is being used as the file upload name and most characters interupt the mapPath function. Also, in the form tag I am calling verifyform(); Can you call more than 1 function in the form or should I call the function StripSymbols() from the verifyform() function?
Do You have any ideas for me? I would really appreciate your help.
Thanks,
Kris
<script language="javascript">
function setColor(el, bg)
{
if (el.style) el.style.backgroundColor = bg;
}
function verifyform()
{
var bgBad = "#F7F28A";
if (trim(document.form.Title.value) == "")
{
setColor(document.form.Title, bgBad);
alert("Please enter a title!");
document.form.Title.focus();
return false;
}
if (trim(document.form.EventDate.value) == "")
{
setColor(document.form.EventDate, bgBad);
alert("Please enter a date!");
document.form.EventDate.focus();
return false;
}
if (trim(document.form.Notes.value) == "")
{
setColor(document.form.Notes, bgBad);
alert("Please enter a brief description!");
document.form.Notes.focus();
return false;
}
if (trim(document.form.Description.value) == "")
{
setColor(document.form.Description, bgBad);
alert("Please enter a description!");
document.form.Description.focus();
return false;
}
return true;
}
function trim(strText)
{
while (strText.substring(0,1) == " ")
strText = strText.substring(1, strText.length);
while (strText.substring(strText.length-1,strText.length) == " ")
strText = strText.substring(0, strText.length-1);
return strText;
}
Function StripSymbols(document.form.Title)
{
Dim nCharPos, sOut, nChar
nCharPos = 1
sOut = ""
For nCharPos = 1 To Len(document.form.Title)
nChar = Asc(Lcase(Mid(document.form.Title, nCharPos, 1)))
If ((nChar > 47 And nChar < 58) or_
(nChar > 96 And nChar < 123) or_
nChar = 32) Then
sOut = sOut & Mid(document.form.Title, nCharPos, 1)
End If
Next
StripSymbols = sOut
}
</script>
I am calling the function like this:
<form ENCTYPE="multipart/form-data" action="Manage_Events-A-Step2.asp" name="form" method="post" onsubmit="javascript:return verifyform();">
For some reason it is ignoring the function. As for the StripSymbols function it is really important because the Title can not have any special characters because it is being used as the file upload name and most characters interupt the mapPath function. Also, in the form tag I am calling verifyform(); Can you call more than 1 function in the form or should I call the function StripSymbols() from the verifyform() function?
Do You have any ideas for me? I would really appreciate your help.
Thanks,
Kris