Forum Moderators: open

Message Too Old, No Replies

email validation function - get warnings by xhtml validation

need some help in fixing the warnings please

         

tito

2:37 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Hello,

I'm using this function and i get warnings when validating my xhtml page, i've tried to replace some parts (& < ...) but if i have to follow the W3C validation the function does not work anymore.

Please do you know some workaround on this? thanks so much!

here's the .js
--------------

<script language="JavaScript" type="text/javascript">
function checkNEmail(form) {
if (isBlank(form.email.value) ¦¦ isBlank(form.name.value) ¦¦!isEmailValid(form.email.value) )
{
alert("Please enter a valid Name and Email Address .\nThe email or name you have typed in does not appear to be valid.");
form.email.focus();
return false;
}
}

function checkEmail(form) {
if (isBlank(form.email.value) ¦¦!isEmailValid(form.email.value) ) {
alert("Please enter a valid Email Address.\nThe email you have typed in does not appear to be valid.");
form.email.focus();
return false;
}
return true;

}

function isBlank(fieldValue) {
var blankSpaces = / /g;
fieldValue = fieldValue.replace(blankSpaces, "");
return (fieldValue == "")? true : false;
}

function isEmailValid(fieldValue) {
var emailFilter = /^.+@.+\..{2,4}$/;
var atSignFound = 0;
for (var i = 0; i <= fieldValue.length; i++)
if ( fieldValue.charAt(i) == "@" )
atSignFound++;
if ( atSignFound > 1 )
return false;
else
return ( emailFilter.test(fieldValue) &&!doesEmailHaveInvalidChar(fieldValue) )? true : false;
}

function doesEmailHaveInvalidChar(fieldValue) {
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\] ]/;
return ( illegalChars.test(fieldValue) )? true : false;
}
</script>

--------------

here's the W3C validation warnings report i'd like to fix:

for (var i = 0; i <= fieldValue.length; i++)
--character "<" is the first character of a delimiter but occurred as data--

return ( emailFilter.test(fieldValue) &&!doesEmailHaveInvalidChar(fieldValue)
--character "&" is the first character of a delimiter but occurred as data--

...turn ( emailFilter.test(fieldValue) &&!doesEmailHaveInvalidChar(fieldValue)
--character "&" is the first character of a delimiter but occurred as data--

var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\] ]/;
--character "<" is the first character of a delimiter but occurred as data--

encyclo

3:01 pm on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's more to do with the way the validator handles inline scripts rather than anything else - it is trying to parse inline Javascript as HTML.

To avoid the problem, you should really be placing the Javascript in a separate file:

<script type="text/javascript" src="myexternalfile.js"></script>

Bernard Marx

3:45 pm on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd follow encyclo's advise, but if you need/want to use an on-page script, the code should be enclosed in CDATA tags. Something along the lines of:

<script type="text/javascript">
// <![CDATA[

// ]]>
</script>

tito

4:13 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Thanks encyclo, yes always separating contents as per w3c rule, i keep forgetting it, Thank you!

Thanks Bernard Marx, i did not know this CDATA tags, very nice workaround!

Thanks so much.