Validating an Email in JavaScript [duplicate] - javascript

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I'm using the pattern:
var pattern = /^[^\s#]+#[^\s#]+\.[^\s#]+$/;
So when I submit/send an email to myself from my contact form to test out a bunch of different email combinations, everything has worked except for:
whatever#yahoo.com && whatever#google.com
I'm not entirely sure why those two aren't being included, but I'd appreciate any assistance.

I guess you miss some chars in your pattern,
you may find here an answer
Validate email address in JavaScript?
there is a code which handles more chars that you forgot, and the comment above too.

I'm not sure whats going wrong with yours, but here's an email pattern I've used successfully.
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$"

This help you :
var patt = /^[A-Za-z0-9]+#[A-Za-z0-9]+\.[A-Za-z]+$/gmi
final code :
<html>
<head>
</head>
<body>
Enter Your Email : <input type="tel" id="email">
<button onclick="isvalid()">Try</button>
<p id="res"></p>
<script>
var res = document.getElementById("res");
var patt = /^[A-Za-z0-9]+#[A-Za-z0-9]+\.[A-Za-z]+$/gmi
function isvalid(){
str = document.getElementById("email").value;
if(patt.test(str))
res.innerHTML = "Email is Valid";
else
res.innerHTML = "Email is InValid";
}
</script>
</body>
</html>

Related

How do you load a separate .html page with Javascript [duplicate]

This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 3 years ago.
I am trying to figure out how to send a user to another page I design after they have input the correct password. I don't know how to load the page with a JavaScript Command. How could i use the code below to open a new page titled insideVault.html?
I have tried many google searches, but cant find any resources to help me out with this.
// login code //
<div class = "container">
<div class = "passwordEnter">
<div class = "text-center">
<form id="login" onsubmit="return passCheck()" method="get">
<p> Enter Password </p>
<input type="password" name="password" id = "password">
<input type="submit">
</form>
</div>
</div>
</div>
<script src="script.js"></script>
</div>
<script>
function passCheck(){
var input = document.getElementById("password").value == 'test';
console.log(input);
if(!input){
alert('Password incorrect, please try again.');
return false;
}
return true;
}
I want the code to load a new page, but so far I have not been able to find any code that would allow me to do this.
Try location.href:
function passCheck(){
var input = document.getElementById("password").value == 'test';
console.log(input);
if(!input){
alert('Password incorrect, please try again.');
return false;
}
location.href = 'insideVault.html';
}

remove all email,external links,contact number and few words Regex

I have an text area in which i want to remove all the emails,extarnal links ,contact numbers and few fixed words.also I want to permit a external link.I have tried for this but has not got success will you help me please.Here is my html
var str=$('#textarea_before').html();
var toagaincheck = str.replace(/(at\)|#|www|http|https).*?(\.|dot\))(com|in|org|uk|ac|!)/g, '');
var last_second_check = toagaincheck.replace(/(\.|dot\))(com|ac|in|org|uk)/g, '');
var last_check = last_second_check.replace(/\d{5,}/g, '');
var last_before = last_check.replace(/google|yahoo|mozilla/g, '');
var result=last_before.replace(/(\(d{5,}|((\d|\s){10,}))/g, '');
$('#textarea_after').html(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows='10' cols='50' id='textarea_before'>
Hi Ravi Prakash Awasthi,
I am ravi from algocrats. persuing my M.Tech from http://www.iitd.ac.in or http://iitd.ac.in I wanna contact You through email or contact number. Can you send me your email or contact me in my email (i.e. ravi.awasthi93#gmail.com). My contact no is 1234587585.I am trying to write my contact like 1 2 3 4 5 8 7 5 8 5.
I want to permit a link that is http://www.rgpv.ac.in but it also being replaced.
</textarea>
<textarea rows='10' cols='50' id='textarea_after'></textarea>
help me to make regex in two max lines and how to permit to include a external link. thanks......
try this JSFiddle
var str=$('#textarea_before').html();
var toagaincheck = str.replace(/\b(http:(?!\/*www\.holidayporch\.com)|www\.\.holidayporch\.com).*?\s|\b[^\s]+#[a-z]+\.[a-z]+\b|\b(\d\s*){10}\b|\b(google|yahoo|mozilla)\b/gi, '');
var result=toagaincheck;
$('#textarea_after').html(result);
Where allow = http://www.holidayporch.com
Just add the fixed word which you want to remove and also phone number length is assumed as 10 if other than this then also mention it too. example if phone length is 7 then (\d\s*){7}

Cannot get JavaScript password regex validation to work

I am trying to validate a user entered password. It can only be letters and numbers and I have to use the match method...even if it isn't the best way...I have to use match(). I am missing something to get the working properly. No number or special characters only letters. I do not know much about javascript.
<script type="text/JavaScript">
function chkPwd() {
var pwd = document.form1.pwd;
var pwdMsg = document.getElementById('pwdMsg');
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.match(regex)) {
pwdMsg.innerHTML = "Must contain letters only!"
pwd.select();
return;
}else{
pwdMsg.innerHTML = "";
}
}
</script>
</head>
<body>
<form name="form1" action="" method="post">
<p> Password: <br>
<input type="text" name="pwd" onchange="chkPwd()" />
<span id="pwdMsg"></span></p>
<p>
<input type="button" value="chkPass" onclick="chkPwd()">
</p>
</form>
<div id="results"></div>
</body>
</html>
Your issue appears to be in this section of code.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.value.match(regx)) {
You are setting the pwd1 to pwd.value, but then on the next line, you are accessing pwd1.value. This means that you are efficiently doing pwd.value.value. Additionally, you are using regx where you should use regex. Also, your if condition does not appear to need a ! in it. I think you mean to do this.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(pwd1.match(regex)) {
If you want letters only you should use this regex:
^[A-Za-z]+$
If you want to have letters and numbers then use:
^[A-Za-z0-9]+$
Btw, as guys pointed out you are using regx instead of regex

Switch statement with Form in Javascript for Number [duplicate]

This question already has answers here:
Switch statement for greater-than/less-than
(10 answers)
Closed 8 years ago.
Please can someone help me with a code doing a switch statement with a form to check
something like:
<script language="JavaScript">
<!--
function verifyPerf(form){
var myEntry = form.number(); //check the edit box number, didn't know how?!
var firstPart = "1. You need to do thing 1";
var endPart = "2. You need to to do thing 2";
switch(myEntry){
case "<= 3000" :
alert(firstPart);
break;
case ">3000 and <9000" :
alert(endPart);
break;
//would like to add 3 more cases
default :
alert('You have entered an invalid performance number');
}
}
-->
</script>
</head>
<body>
<form name="myForm">
<b>Please enter your performance number:</b><br>
<input type=number value="" name="perfNumber">
<input type=BUTTON value="Verify" name="myButton" onClick='verifyPerf(this.form)'>
</form>
</body>
</html>
thanks for your help.
Lx
There is alos this part that's not working.
function verifyPerf(form){
var myEntry = form.value;
.....
and the form:
Please enter the Performance:
Your switch statement is looking for the string '<= 3000' or '>3000 and <9000' not a number.
Check this out: Switch on ranges of integers in JavaScript
This demonstrates how to properly switch against a range of numbers.

Unable to Limit the user input in JavaScript [duplicate]

This question already has answers here:
How to impose maxlength on textArea in HTML using JavaScript
(16 answers)
Closed 8 years ago.
I tried limiting the user input but it wasn't successful, please guide me where I am making mistake.
JS
<script type="text/javascript">
function countLength() {
var maxLength=10;
var length = document.getElementById("txt").value.length;
if(length>10) {
return false;
}
}
</script>
HTML code
<form name="formA" id="formA" action="#" >
<textarea id="txt" name="txt" onkeyup="countLength()"></textarea>
</form>
Your code basically replicates the maxlength attribute, which seems to work (and I don't think is being deprecated?). Just use that.
<input type='text' name='mytext' maxlength='10'>
return false on onkeyup does nothing (as you've probably noticed). I've seen solutions where someone would just alter the value of the textarea, perform a substring operation, and assign that new value back.
Try this:
function countLength() {
var maxLength=10;
var ta = document.getElementById("txt");
var length = ta.value.length;
if(length>maxLength) {
ta.value = ta.value.substr(0, maxLength);
return false;
}
}

Categories

Resources