I am doing email regular expression validation for the email and I am stuck on one point using
following expression if user enter something like abc.abc then this expression works fine but when user enters advxyz#pqr.com then it doesn't work
var myreg = new RegExp(/([a-z0-9.]+)#([a-z0-9]+)\.([a-z0-9]+)/i);
var patter = myreg.test(document.getElementById("email").value)
alert(patter)
if(patter == false){
errorMsg.push("email Formate Error Ex:firstname.lastname#abc.com");
}
I want that user must enter his email in this formate like firstname.lastname#abc.com/.ca/.org
var pattern = /^[a-z0-9]+\.[a-z0-9]+#[a-z0-9]+\.[a-z]+$/i;
pattern.test('firstname.lastname#abc.com');
// returns true;
pattern.test('jsmith#abc.com');
// returns false;
pattern.test("abc.abc");
// returns false;
Using Regular Expressions is probably the best way.
Here's an example
(live demo):
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\ ".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
Source
This work Perfectly for me.
/^[a-zA-Z]([a-zA-Z0-9_\-])+([\.][a-zA-Z0-9_]+)*\#((([a-zA-Z0-9\-])+\.){1,2})([a-zA-Z0-9]{2,40})$/;
Hope this is helpful in resolving the issue \b[A-Z0-9._%-]+#[A-Z0-9.-]+.[A-Z]{2,4}\b
Its case sensitive.
Pattern for to validate email address is:
/^([a-zA-Z0-9_.-])+\#(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
use this pattern in Regex. and you can validate any email..
Related
In my asp.net web application. I need to validate a textbox entry to avoid these special characters \/:*>"<>|.I planned to replace the character with empty string, and for that wrote a javascript function and addded the attribute to call the function from server side as below
txtProjectName.Attributes.Add("onkeyup", "ValiateSpecialCharacter()");
As of this every thing is fine and the function is called.while enter any character. The function is
function ValiateSpecialCharacter(){
var txt=document.getElementById("<%=txtProjectName.ClientID%>").value;
txt.replace(/[\\\/:*>"<>|]/g, '');
alert(txt);
document.getElementById("<%=txtProjectName.ClientID%>").value=txt;
}
I use a regular expression in the function to do this. But the test is not getting replaced as planned. Is there any mistake in this code.Also note that the alert is working.
Try to get the result in txt ie, get the value of replaced text inside your variable.
txt = txt.replace(/[\\\/:*>"<>|]/g, '');
In your query you getting previous value.Assign properly like this txt = txt.replace(/[\\\/:*>"<>|]/g, '');.It show the latest result in alert box.
function ValiateSpecialCharacter(){
var txt=document.getElementById("<%=txtProjectName.ClientID%>").value;
txt = txt.replace(/[\\\/:*>"<>|]/g, '');
alert(txt);
document.getElementById("<%=txtProjectName.ClientID%>").value=txt;
}
This is not what you asked, but seems like a strange way to go about your needs. Unless, I misunderstood the question. Since you are running ASP.NET on the server, why use JavaScript for server validation?
It usually does make sense to validate input on the client. For that, you need to hook an event like form submit to call the javascript function.
If you want to validate on the server, use something like, inside a function handling form submit:
Regex re = new Regex("^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{1,})+$");
if (!re.IsMatch (domain.Text)) {
warningLabel.Text = "Domain format is invalid!";
formError = true;
}
Obviously, you don't validate the domain so change the regex etc. No JavaScript is needed for server-side validation.
Basically I created a form in html and when things are input properly it simply goes to google.com. Right now I have completed the first few fields but I am unsure of how I would make it recognize if the input that was put in and did not include an # sign or a . some point after it.
I created a fiddle as I was having trouble getting some of the longer of my lines of code to be in-line.
Click here for the fiddle Example
You have a few options depending on how thorougly you want to validate.
email.indexOf('#') >= 0
checks that there is an # at all in the email. See http://jsfiddle.net/27f1h6ws/ for a version of your fiddle with it added.
A more thorough way would be to check it with regex. You can do it extremely simple just checking the general structure of the email input, or extremely thorough check for all valid characters, depending on how crucial the validation is. See this link or the answers in this question for more information.
You can use HTML5 properties like :
pattern attribute which contains a regexp
set your input type to email
If you want to do it with JavaScript, use a regexp also and use the test() method to verify it.
Add this
var re = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 8 years ago.
I am very new to coding so go easy.
I am trying to make a email validation form but it needs to reject a blank cell (input box) sorry for being so bad at coding..... i also was going to use a regex
it has to be alpanumeric#alpanumeric.alpanumeric
sorry
The correct behavior in this case would be to perform a "pre-check" on fields before actually executing some more complex validation (eg: regular expressions).
The logic would look something like this:
valid_email = false;
email = strip_leading_trailing_spaces( email ); // don't forget to cleanup user input
if ( email != "" ) {
// perform regex testing here, set valid_email to false if failed
}
// handle "valid_email" variable here
It's worth noting here that any client side validation should be duplicated to/re-checked on the server as any user with a little knowledge in JS could easily bypass any validation done on the clients computer.
You don't need a regex if you are just checking to see if it's empty:
<input type=text id=email><button onClick="validate()">Validate</button>
<script language="javascript">
function validate() {
if ($("#email").val().length == 0) {
alert("Enter an email address");
}
}
</script>
with a regexp :
var valid_email = ! email.match( /^\s*$/ ) ;
If there's only spaces and tabs or nothing then valid_email = false
demo : http://regex101.com/r/iX8lF7/1
This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 8 years ago.
I am having trouble with my email validation code using function validateEmail (str). Any suggestions?
//validates email address form
function validateEmail (str)
{
var retVal;
var atpos=retVal.indexOf("#");
var dotpos=retVal.lastIndexOf(".");
if (atpos<1 || dotpos<atpost+2 || dotpos+2>=retVal.length)
{
retVal = false;
return retVal;
}
Any suggestions? Yes. Don't.
<input type="email" />
Problem solved!
It should be noted that use of HTML5 features will make for a beautiful web... provided the browser supports it. The reason being, a failed validation will cause the browser to notify the user of the error in a non-intrusive and consistent, native manner.
Older browsers will not be able to validate in this way, however due to the specification stating that unrecognised type values should default to text, HTML5 is fully backwards compatible, way back into IE5.5 and almost certainly even earlier - maybe even IE1!
This lack of validation is not an issue. Validation should always be handled server-side, regardless of what validation you have on the client-side. As an example, in PHP, you would pass it through filter_var with the FILTER_VALIDATE_EMAIL filter.
The Problem of your code is that you create an empty variable (var retVal) and then you check this variable instead of the given string:
var retVal;
var atpos=retVal.indexOf("#");
var dotpos=retVal.lastIndexOf(".");
The correct code is
function validateEmail (str)
{
var retVal;
var atpos=str.indexOf("#");
var dotpos=str.lastIndexOf(".");
if (atpos<1 || dotpos<atpost+2 || dotpos+2>=str.length)
{
retVal = false;
return retVal;
}
// further code
}
I'm sure there must be a simple solution to this..
I simply want to create a rudimentary human verification tool for an online form. In pseudo-code, something like
$answer = "foo";
if (form['question'] == $foo){
// Proceed
} else {
// Fail
}
The jQuery docs seem to have an equalTo method but this is to compare a form field with another form value..
Any tips greatly appreciated! :)
You need to have a look at JQuery Validation plugin at http://docs.jquery.com/Plugins/validation. Consider the following example:
You may change the rules as explained in the plugin to implement your custom validations.
Have you looked at this plugin? You can use it to make fields required, validate for certain value types (number, string, credit card, etc), and I believe you can write callback functions to validate for specific values.
http://docs.jquery.com/Plugins/validation
var answer = 'foo';
if ($('#question').val() == answer) {
// Proceed
} else {
// Fail.
}