Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

blank field and special characters validation

Status
Not open for further replies.

Kris912

Programmer
Joined
Jul 29, 2005
Messages
27
Location
US
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

 
1. you've written StripSymbols in VB, not javascript
2. you're not calling StripSymbols anywhere in your example code
3. you can simplify StripSymbols tremendously using regular expressions:
Code:
function StripSymbols(text) {
	//  strip any non-alphanumeric characters
	return text.replace(/[^a-z0-9]/gi, "");
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
You can use this function to validate that only alphanumeric characters are present. It does not strip characters.

function checkstring(inputStr)
{
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var checkStr = inputStr;
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 && inputStr != "") {
return true;
}
else
{
alert('Field must not be blank or contain special characters.');
}
}


Paranoid? ME?? WHO WANTS TO KNOW????
 
NiteOwl,

This might sound like a stupid question but the variable "inPutStr" doesn't that get replaced by document.form.title.value to send it the value of the form field I am trying to validate?

And in my form tag I put onsubmit="javascript:return checkString();

Thanks for you help!!

Kris :)
 
Yes, you can do it that way. Declare the value of inputStr right within the function instead of passing it in.
If you ever want to use this function to check other fields as well then it would be better to pass the value in either directly from your form page or from another function.

Putting the call into onsubmit like you have though will limit testing to that one single field. Do you have any other fields you have to test in some way? If you do then setup a different function to do validation and within that function call the checkstring function.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top