PDA

View Full Version : what is this



ahmedsoliman
06-19-2002, 12:02 AM
please explain to me this code ,idont under stand it specially
function check email()



<form name="validation" onSubmit="return checkbae()">
Please input a valid email address:<br>
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
</form>
<script language="JavaScript1.2">

//Advanced Email Check credit-
//By JavaScript Kit (www.javascriptkit.com)
//Over 200+ free scripts here!

var invalidaddress=new Array()
invalidaddress[0]="hotmail"
invalidaddress[1]="rocketmail"
invalidaddress[2]="yahoo"
invalidaddress[3]="zdnetmail"
//extend or shorten this list if neccessary

var testresults
function checkemail(){
var invalidcheck=0;
var str=document.validation.emailcheck.value
var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)){
var tempstring=str.split("@")
tempstring=tempstring[1].split(".")
for (i=0;i<invalidaddress.length;i++){
if (tempstring[0]==invalidaddress[i])
invalidcheck=1
}
if (invalidcheck!=1)
testresults=true
else{
alert("Please input a more official email address!")
testresults=false
}
}
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}
</script>

<script>
function checkbae(){
if (document.layers||document.getElementById||document.all)
return checkemail()
else
return true
}
</script>

<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>

bpaulsen2000
06-19-2002, 04:44 AM
hi,

Description: A script that closely examines the content of a form box to ensure that the user entered a valid email address. If not, the form submition is canceled, and the surfer prompted to re-enter a valid address. The script makes the following assumptions regarding what a valid email address is:

-Contains a least one character preceding the "@"
-Contains a "@" following the preceding character(s)
-Contains at least one character following the "@", followed by a dot (.), followed by either a two character or three character string (a two character country code or the standard three character US code, such as com, edu etc)

Note that this script requires NS 4+ or IE 4+ to function- all other browsers will simply not participate in the form validation process.

ahmedsoliman
06-19-2002, 08:33 AM
ok iknow that but idon't understand the following code
it's strang for me
what the purpose of (split,test,using array,strange string in var filter)
-----------------------------------------------------------------------

var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)){
var tempstring=str.split("@")
tempstring=tempstring[1].split(".")
for (i=0;i<invalidaddress.length;i++){
if (tempstring[0]==invalidaddress[i])
invalidcheck=1

JohnKrutsch
06-19-2002, 04:34 PM
filter is a variable that uses regular expressions to put these rules:


-Contains a least one character preceding the "@"
-Contains a "@" following the preceding character(s)
-Contains at least one character following the "@", followed by a dot (.), followed by either a two character or three character string (a two character country code or the standard three character US code, such as com, edu etc)


into one line of code. Regular expressions are very helpful but as you see can get a bit complex.

test() checks to see if something matches the rules set up in the regular expression. Here is a simple example:

<script type="text/javascript">
str="567"
filter=/^\d+$/
alert(filter.test(str))
</script>


The string "str" is just a bunch of numbers in this case. The filter is a regular expresion that says:

^ (start at the beginning)
\d+ (there can only be one or more numbers
$ (end at the end of the string)

Try adding a leter to str to see what happens. I am not as strong in regular expressions as I would like to be but I strongly suggest pciking it up.

MikoLone
06-19-2002, 10:39 PM
Is there a good tutorial somewhere on learning it?

TrueLies
06-19-2002, 11:21 PM
just check for, say, "javascript regular expressions" on search engines. Originally, reg expressions were a Perl thing.
Probably their worst feature is the so called greed. Once you stumble across greed after you've learned them (they're not really difficult, just odd at sight), a file that may help you understand greed at least the way I understood it is on my site (spellbinders section, "Understanding Greed in Regular Expressions", with also a small form where you can test your own regular expression skills)

In JavaScript we have several methods that work with regular expreession: match, test, exec... you may want a manual. Goodman' JS Bible is the best in my opinion, also Flanagan's good.

whammy
06-20-2002, 02:13 AM
http://developer.netscape.com/docs/manuals/js/core/jsguide15/regexp.html#1010922

http://www.siteexperts.com/tips/functions/ts23/page1.asp

http://www.evolt.org/article/rating/20/22700/

ASP: http://www.4guysfromrolla.com/webtech/RegularExpressions.shtml