JavaScript - Validate Email Field For Commas
Hello:
I have an expression validating email addresses but it seems there is a loophole. If a user enters a comma this is accepted. Can anyone tell me how i can modify the following to disallow commas? validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i; strEmail = document.form1.df_email1.value; if (strEmail.search(validRegExp) == -1) { alert("A valid e-mail address is required."); document.form1.df_email1.focus(); return false; } Similar Tutorialshere is my current email validation code: function validateEmail(strValue) { var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i; return objRegExp.test(strValue); } what should i add to reject email addresses from hotmail.com and yahoo.com? hiiii, im new to javascripts . Can anyone help me to know that a javascript which is validating a phone number accepts only digits but if the text field is left empty it should accept the entry as an empty entry.... plz share knowledge u will be more knowledgeable [ICODE] <html> <head> <title>ABS</title> <?php $j="dove"; ?> <script language="JavaScript" type="text/javascript"> <!-- function checkform ( form ) { if (form.Name.value == "") { alert( "Please enter your email address." ); form.Game.value="<?php echo $j;?>"; form.Name.focus(); return false ; } if (form.Game.value == "") { alert( "Please enter your game address." ); form.Game.focus(); return false ; } if (document.form.Shame[0].value== "") { alert( "Please enter your game address." ); form.Game.focus(); return false ; } return true ; } </script> </head> <body> <form name="form" action="Store_it.php" method="post" onSubmit="return checkform(this);"> <input type="text" name="Name" /> <input type="text" name="Game" /> <input type="text" name="Shame[]" /> <input type="text" name="Shame[]" /> <input type="text" name="Shame[]" /> <input name="submit" type="submit"> </form> </body> </html> [icode] I need to use the array's shame and just check if the third one is not empty. I have searched DOM and other stuff but i cannot get to the 2 element of the array shame. How do i check the value of the array. I have this script to validate contact form fields. It works fine for checking that fields are not blank. I added the valid email code, which I copied from the w3schools web site. But, it does not work. I am confused about how to reference the email form field name. That is really the only thing I changed, that is, I added the frm.elements to the email variable. Can anyone please help? Thanks! Code: function validate_form(frm) { frm.elements.name.className = "input"; frm.elements.email.className = "input"; frm.elements.subject.className = "input"; frm.elements.comments.className = "input"; var errors=0; // // NAME cannot be blanks // if (isBlank(frm.elements.name.value)) { frm.elements.name.className = "inputreq"; errors++; } // // EMAIL ADDRESS cannot be blanks // if (isBlank(frm.elements.email.value)) { frm.elements.email.className = "inputreq"; errors++; } // // SUBJECT cannot be blanks // if (isBlank(frm.elements.subject.value)) { frm.elements.subject.className = "inputreq"; errors++; } // // COMMENTS cannot be blanks // if (isBlank(frm.elements.comments.value)) { frm.elements.comments.className = "inputreq"; errors++; } // // EMAIL ADDRESS appears invalid // if (validate_email(frm.elements.email.value,"Not a valid e-mail address!")==false) { alert("TEST"); frm.elements.email.focus(); return false; } // // Errors // if (errors>0) { alert("Please enter all fields!"); return false; } //No errors //frm.submit(); } function isBlank(value) { regexp = /^\s*$/ return (value==null || regexp.test(value)); } // Validate email address function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } The below code works ok but need our help to get it perfect. I want the function to validate that after the "@" the user types "wendys.com". Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>New Page 1</title> <SCRIPT> function validateLP(theForm){ if(theForm.DM_Email){ var checkEmail =theForm.DM_Email.value; var invalidChars = " /:,;" if (checkEmail == "") { alert("Please enter a valid Wendys DM email address."); theForm.DM_Email.style.backgroundColor='#FFFF99'; theForm.DM_Email.focus(); return false; } if (checkEmail != "") { // can be empty for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters? var badChar = invalidChars.charAt(i) if (checkEmail.indexOf(badChar,0) > -1) { alert("Please enter a valid Wendys DM email address."); theForm.DM_Email.style.backgroundColor='#FFFF99'; theForm.DM_Email.focus(); return false; } } var periodPos1 =checkEmail.indexOf(".",1) // there must be one "." symbol var atPos = checkEmail.indexOf("@",periodPos1-1) // there must be one "." symbol before "@" var periodPos2 = checkEmail.indexOf(".",atPos) //periodPos2 = checkEmail.indexOf(".",atPos) if ( (atPos == -1) || (periodPos2 == -1)){ //return false alert("Please enter a valid Wendys DM email address."); theForm.DM_Email.style.backgroundColor='#FFFF99'; theForm.DM_Email.focus(); return false; } } } } </SCRIPT> </head> <body> <form method="POST" NAME="theForm" onSubmit="return validateLP(theForm)" action="--WEBBOT-SELF--"> <p><input type="text" name="DM_Email" size="20"></p> <p> </p> <p> </p> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body> </html> I am Gururaj. I have written a form in HTML which contains username,lastname,email,password and submit. I have written a javascript to validate this form [validate(username,lastname,email,password)]. On submit this javascript function will be called and hence form get validated. I am passing all the arguements(username,lastname,email and password) to the javascript function.. Is it possible to make me a code such that it should call an individual function for each field in the form.for ex:if i have not entered username, last name in the form and attempt to submit, only username arguement should be passed to the function as it comes first in the form. In other sense i want to validate individual text field validation of my form. So can anybody help me.It will be very needful to me. alright so i am trying to validate this form and if you do not fill out the Day of the week then it does not submit the form to email.... Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Guest List Signup</title> <style type="text/css"> <!-- body,td,th { color: #FFF; } body { background-color: #000; } --> </style></head> <script type='text/javascript'> function madeSelection(elem, helperMsg){ if(elem.value == "Select"){ alert(helperMsg); elem.focus(); return false; }else{ return true; } } </script> <body bgcolor="#000000" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF"> <form name="halo-gl" form method="POST" action="contactgl.php"> <p>If you want to have your name on the HALO guest list please fill out the form below before 9pm and you and your guests will be on the list at the door. <p>This will get you free entry* until 11:30pm (this means at 11:31pm the list is over) also on select events there will not be a list and we might not have it posted on the website. <p>Guest list does not guarantee entry. It is to the discression of Halo on wether someone is permitted or not. <p> <p>Your Name:<br> <input type="text" name="MyName"> (First and Last) <p>How Many People I Will Have:<br> <select name="HowManyPeopleIWillHave"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <p>What day to be on the list:<br> <select id='selection'> <option value="Select">Please Select</option> <option value="Wednesday">Wednesday</option> <option value="Thursday">Thursday</option> <option value="Friday">Friday</option> <option value="Saturday">Saturday</option> </select> <p><input type="submit" name="submit" value='Submit' onclick="madeSelection(document.getElementById('selection'), 'Please Choose Something')" > </form> <p> </body> </html> you can see this currently in action at http://www.haloclt.com/guestlist.html any help would be appreciated thank you Hi, I want to check the email address type in a contact form but want to reject it if it's from a certain domain I actually use this regexp : /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/ (taken from the jquery validationengine) to check if the email address is correctly formated but I'd like to reject it if contains hotmail between the @ and the . I tried many things but couldn't get something that works. If anyone has an idea, it would be greatly appreciated. Chag Hi there, I would like to validate the email address typed into the prompt message by the user, but to no avail. Can some kind soul help? Code: function addOption() { var new = prompt("Enter New Item:"); if (!new == "") { var answer = confirm ("Are you sure you want to add? ") if (answer)//if answer is true { var lst = document.getElementById('lstBx'); // listbox control id // Now we need to create a new 'option' tag to add to MyListbox for (var i = 0; i < lst.options.length; i++) { arrTexts = lst.options[i].text; if (arrTexts.toLowerCase() == newItem.toLowerCase()) { alert ("That email address is already included in the list - please enter another one."); break; } else { validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i; strEmail = lst.value; // search email text for regular exp matches if (strEmail.search(validRegExp) == -1) { alert('A valid e-mail address is required.\nPlease retry.'); return false; } var optionNew = document.createElement("option"); optionNew.value = new; // The value that this option will have optionNew.innerHTML = new; // The displayed text inside of the <option> tags // Finally, add the new option to the listbox lst.appendChild(optionNew); //sort items in listbox in alpha order arrTexts = new Array(); for(i=0; i<lst.length; i++) { arrTexts[i] = lst.options[i].text; } arrTexts.sort(); for(i=0; i<lst.length; i++) { lst.options[i].text = arrTexts[i]; lst.options[i].value = arrTexts[i]; } } return false; } } } else { if(new == "") { alert("Key something to textbox please."); } else alert("Cancelled."); } } Code: <select id="lstBx" name="listBox" size="6" style="width: 580px;"> <option>a@hotmail.com</option> <option>b@hotmail.com</option> <option>c@yahoo.com</option> <option>d@gmail.com</option> <option>e@ymail.com</option> <option>f@msn.com</option> </select> Hi! I would like to show you guys the JavaScript that i using for my contact form /*----- Code goes here by clicking the link -----*/ http://web.ezenne.com/js/completeValid.js The above code was copied from a Youtube tutorial about JavaScript. I would like to ask, what additional code should be added in the existing code in order to validate the numbers only and an email format before the form to be submitted? Thanks! Regards, Ezenne Hello, I'm trying to make sure that my email field contains at least 5 characters and the @ symbol. I've been trying to do this using an if...else loop, but so far I've only been running into brick walls. I think my problem is I don't know what to use when it comes to document.form1.email.value This is the latest script I tried: Code: <html> <head> <title>Form Example</title> <script language="JavaScript" type="text/javascript"> function validate() { if (document.form1.yourname.value.length < 1) { alert("Please enter your full name."); return false; } if (document.form1.address.value.length < 3) { alert("Please enter your address."); return false; } if (document.form1.phone.value.length < 3) { alert("Please enter your phone number."); return false; } if (document.form1.email.value.length < 5) alert("Please enter at least 5 characters."); else if (document.form1.email.value.match(@)) alert("Please enter an @ symbol."); return false; return true; } </script> </head> <body> <h1>Form Example</h1> <p>Enter the following information. When you press the Submit button, the data you entered will be validated, then sent by email.</p> <form name="form1" action="mailto:user@host.com" enctype="text/plain" method="POST" onSubmit="return validate();"> <p><b>Name:</b> <input type="TEXT" size="20" name="yourname"> </p> <p><b>Address:</b> <input type="TEXT" size="30" name="address"> </p> <p><b>Phone: </b> <input type="TEXT" size="15" name="phone"> </p> <p><b>Email Address: </b> <input type="TEXT" size="30" name="email"> </p> <p><input type="SUBMIT" value="Submit"></p> </form> </body> </html> Hello I have a form validation script which checks for empty fields and an email address. The empty fields part seems to work, but not the validation (as long as I type characters in the email field, it seems to be acceptable). Here is what I have: Code: //function to check for empty fields function isEmpty(strfield1, strfield2, strfield3) { //change "name, email and subject" to your field names strfield1 = document.forms[0].ContactUs_Name.value strfield2 = document.forms[0].ContactUs_Email.value strfield3 = document.forms[0].ContactUs_Subject.value //name field if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') { alert("\"Name\" is a mandatory field.\nPlease amend and retry.") return false; } //url field if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ') { alert("\"Email\" is a mandatory field.\nPlease amend and retry.") return false; } //title field if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ') { alert("\"Subject\" is a mandatory field.\nPlease amend and retry.") return false; } return true; } //function to check valid email address function isValidEmail(strEmail){ validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i; strEmail = document.forms[0].email.value; // search email text for regular exp matches if (strEmail.search(validRegExp) == -1) { alert('A valid e-mail address is required.\nPlease amend and retry'); return false; } return true; } //function that performs all functions, defined in the onsubmit event handler function check(form){ if (isEmpty(form.ContactUs_Name)){ if (isEmpty(form.ContactUs_Email)){ if (isEmpty(form.ContactUs_Subject)){ if (isValidEmail(form.email)){ return true; } } } } return false; } </script> </head> <body bgcolor="#ECECEC" text="#000000"> <h3>Form validation with JavaScript</h3> <form name="theform" method="post" action="#" onSubmit="return check(this);"> Name:<br /> <input name="ContactUs_Name" type="text" /><br /> E-Mail:<br> <input name="ContactUs_Email" type="text" id="ContactUs_Email" /> <br /> Subject:<br> <input name="ContactUs_Subject" type="text" id="ContactUs_Subject" /> <br> I just wondered, is it here that the problem lies: if (isValidEmail(form.email)){ Thanks. Steve I have a web form that requests a user full name and email address. What I would like to do is when the user fills in the full name in one input box, I would like to take the full name and populate the email address field as such. "first.last@allstate.com". So pretty much split the full name and popluate email address field "first.last@allstate.com". Tracy i'm using a shopping cart template and i do NOT want to require that shoppers have to enter their email address. how do I disable the requirement, but still ensure if they do type it in, that the format is correct (i.e. person@website.com)? thanks! PHP Code: <?php if(! is_array($alladdresses)){ ?> if(frm.email.value==""){ alert("<?php print $xxPlsEntr?> \"<?php print $xxEmail?>\"."); frm.email.focus(); return (false); } validemail=0; var checkStr = frm.email.value; for (i = 0; i < checkStr.length; i++){ if(checkStr.charAt(i)=="@") validemail |= 1; if(checkStr.charAt(i)==".") validemail |= 2; } if(validemail != 3){ alert("<?php print $xxValEm?>"); frm.email.focus(); return (false); } Hey All, Been having some trouble with this, I am trying to find a javascript or DHTML form field which is similar to the outlook web access email address fields which show an icon to identify the address, and the screen name of the address instead of the full email address, as a hyperlink. I was wanting to know if anyone knows of something like this already around or if I need to design my own. Thanks in advance. Hello all, I have a form that submits a POST request when data is submitted. A Servlet then processes this POST request and a JavaBean is used to make some calculations. The HTML response is not generated within the Servlet but instead I forward the request to a JSP to generate the response. - This all works fine, thankfully. However, I am stupidly suck trying to validate the form on the client side with a JavaScript function before the form is submitted. Here is my index.jps: Code: <%-- Document : index Created on : 19-Nov-2009, 13:41:30 Author : lk00043 --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/JavaScript"> <!-- Start hiding JavaScript Statements function validateForm() { var student; var score1, score2, score3, score4; student = document.getElementById('student'); s1 = document.getElementById('score1'); s2 = document.getElementById('score2'); s3 = document.getElementById('score3'); s4 = document.getElementById('score4'); score1 = parseInt(s1.value); score2 = parseInt(s2.value); score3 = parseInt(s3.value); score4 = parseInt(s4.value); if(student.value.length == 0) { document.getElementById('StudentError1').innerHTML = " Enter a student name!"; return false; } if ((isNaN(score1)) || (score1 < 0) || (score1 > 100)) { document.getElementById('Error1').innerHTML = " Enter a number between 0 and 100!"; return false; } if ((isNaN(score2)) || (score2 < 0) || (score2 > 100)) { document.getElementById('Error2').innerHTML = " Enter a number between 0 and 100!"; return false; } if ((isNaN(score3)) || (score3 < 0) || (score3 > 100)) { document.getElementById('Error3').innerHTML = " Enter a number between 0 and 100!"; return false; } if ((isNaN(score4)) || (score4 < 0) || (score4 > 100)) { document.getElementById('Error4').innerHTML = " Enter a number between 0 and 100!"; return false; } } // End hiding JavaScript Statements --> </script> <title>Lab Class 7 - Task 2</title> </head> <body> <h1>Lab Class 7</h1> <form name="collectgrades" action="AssessGrades" method="POST" onSubmit="validateForm()" > Name of Student: <input type="text" name="student" id="student"/><span id="StudentError1"> </span><br /> Presentation: <input type="text" name="score" id="score1"/><span id="Error1"> </span><br /> Writing style: <input type="text" name="score" id="score2"/><span id="Error2"> </span><br /> Technical content: <input type="text" name="score" id="score3"/><span id="Error3"> </span><br /> Depth of analysis: <input type="text" name="score" id="score4"/><span id="Error4"> </span><br /> Feedback:<select name="feedback" size="4" multiple="multiple"> <option>"Could be better structured."</option> <option>"Depth of analysis is good."</option> <option>"Very advanced material."</option> <option>"Very well structured."</option> </select><br /> <input type="submit" value="Submit" /> </form> </body> </html> Regardless of whether incorrect input is given, the data is still POSTed to the server and calculated on or a Server Side error is given. Am I correct in calling the function onClick? The validation essentially needs to be so that: - Student field contains a string - Score1, Score2, Score3 and Score 4 contain a number between 0 and 100 Any help is most appreciated, Cheers, Beetle. Does anyone know of a good library/function that will add the commas to a number automatically as the number is entered into a field?
Is there a built in function to format a number to 2 decimal places and to add commas like below. 2,222.33 30,033.98 1,222,345,99 I frequent a site that involves typing huge numbers. And I'm thinking of making a userscript that automatically adds commas as I type numbers on all the input boxes. There are multiple input boxes and it has two types: First input type has similar name ; Second input type has similar class. Here's an example of a page: Code: <input type="text" value="0" name="buy_price"><br/> <input type="text" value="0" name="buy_price"><br/> <input type="text" value="0" name="buy_price"><br/> <br/> <input type="text" value="0" class="money-input"><br/> <input type="text" value="0" class="money-input"><br/> <input type="text" value="0" class="money-input"> My code so far. But it doesn't seem to work. The main function was taken from here. Code: function add_commas(number){ //remove any existing commas... number=number.replace(/,/g, ""); //start putting in new commas... number = '' + number; if (number.length > 3) { var mod = number.length % 3; var output = (mod > 0 ? (number.substring(0,mod)) : ''); for (i=0 ; i < Math.floor(number.length / 3); i++) { if ((mod == 0) && (i == 0)) output += number.substring(mod+ 3 * i, mod + 3 * i + 3); else output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3); } return (output); } else return number; } function addattribute() { //adds onkeyup and removes zero of first input type document.getElementsByName("buy_price")[0].setAttribute("onkeyup", "this.value=add_commas(this.value);"); document.getElementsByName("buy_price")[0].removeAttribute('value'); //adds onkeyup and removes zero of second input type document.getElementsByClassName("money-input")[0].setAttribute("onkeyup", "this.value=add_commas(this.value);"); document.getElementsByClassName("money-input")[0].removeAttribute('value'); } window.onload = addattribute; EDIT: I believe the function function add_commas(number) works. But the problem is I need to find a way to add onkeyup="this.value=add_commas(this.value);" to the input boxes mentioned. EDIT #2: Well I guess I'm back to 0.The function function add_commas(number) seems to only work if it's on the actual html file inside <script> tags. But once you separate it and apply it as a userscript, it doesn't seem to function. Hello my first post. I have a function that insert commas via onclick or onblur. Code: function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } I also am using code from this link, where I can specify what parameters to allow the user to enter only positive/negative, decimals values and restricting characters that are not allowed. http://www.mredkj.com/tutorials/validate2.html I am having a hard time to incorporate the addCommas function into the link above. Does anyone have an example that I can look at where I can have commas added to a value dynamically while typing? e.g. if I type 1000 after that last 0 keystroke, it turns the value into 1,000. If I add 3 more zeros it turns it into 1,000,000. In the same vain if I subtract a 0 from the 1,000,000 the value is 100,000 instead. I appreciate any assistance in advance. |