kas Posted May 22, 2008 Share Posted May 22, 2008 This is supposed to be a script to validate a email address in a textbox. Can anyone help me edit this with the following: • correct indentation • meaningful identifiers (variable, function and control names) • correct use of Hungarian notation for variable identifiers the textbox identifier • suitable comments • appropriate spacing <script language = "javascript"> function a() { var v1 = B.value; var n1; var n2 = 0; for (n1= 0; n1 < v1.length; n1++) { if (v1.charAt(n1) == "@" ) { n2++; } } if ((n2 != 1) || v1.charAt(0) != "") { alert( "Your e-mail address is incorrect”); } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/106756-need-help-with-simple-javascripting-coursework/ Share on other sites More sharing options...
wrongmove18 Posted May 22, 2008 Share Posted May 22, 2008 you can use this.. function a(){ var v1 = B.value; if(!v1.match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$")) alert("Your e-mail address is incorrect"); } Quote Link to comment https://forums.phpfreaks.com/topic/106756-need-help-with-simple-javascripting-coursework/#findComment-547254 Share on other sites More sharing options...
KevinM1 Posted May 22, 2008 Share Posted May 22, 2008 This is supposed to be a script to validate a email address in a textbox. Can anyone help me edit this with the following: • correct indentation • meaningful identifiers (variable, function and control names) • correct use of Hungarian notation for variable identifiers the textbox identifier • suitable comments • appropriate spacing As much as I dislike doing other people's homework, the following should suffice: <!-- Note: language attribute is deprecated. Use type attribute instead. --> <script type="text/javascript"> /** *Function that validates an e-mail address input * *Arguments: *None. * *Return value: *None (alert message upon failure). */ function validateEmail() { var emailAddr = emailInput.value; //value of the input var hasAt = 0; //does the value have an '@'? for(var i = 0; i < emailAddr.length; i++) //iterate through the address to check for the '@' { if(emailAddr.charAt(i) == "@") //if the address has an '@' { hasAt++; //set it to true } } //since hasAt is an integer, it can be treated as a boolean if(!hasAt || emailAddr.charAt(0) != "") //if there's no '@' or the first character is blank { alert("Your e-mail address is not valid."); //alert validation failure } } </script> You should be careful about using charAt() to check a value's length. Just because the first character in a string is blank, it doesn't necessarily mean the entire form input is blank. You should probably check to see if the entire length of the value (in this case, emailAddr.length) is equal to 0. Not only that, but even if an inputed value has an '@', that doesn't mean it's a valid e-mail address, either. I mean: @hello_kitty Isn't a valid address, yet your function will treat it as one. Quote Link to comment https://forums.phpfreaks.com/topic/106756-need-help-with-simple-javascripting-coursework/#findComment-547262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.