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

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}

Related

Convert percentage symbols to u+hex (javascript)

I need to replace decoded string which has percentage symbols to U+hex.
String:
"text=%F0%9F%98%8A&id=60&tags=";
What I need:
change %F0%9F%98%8A to 'U+1F60A' or 1F60A (globally), according to http://unicode.org/emoji/charts/full-emoji-list.html.
For 1f60a:
var c = decodeURIComponent("%F0%9F%98%8A").codePointAt(0).toString(16);
Note that \u1f60a wont work in JS (although it will as an HTML entity), you need 2 codepoints; c.charCodeAt(0).toString(16) & c.charCodeAt(1).toString(16)
var message = "This is my message %F0%9F%98%8A and I love emojis!";
$("p").text(decodeURI(message));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
var message

Validating an Email in JavaScript [duplicate]

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>

Parsing Credit Card data from magnetic stripe reader using javascript

Ok, So I have an html form that is displayed like so:
<span style='required'>*</span> - Indicates required field.
<div class='fields'>Swiped Information</div>
<input type=text name='swiped' id='swiped'>
</div>
<div class='fields'>First Name</div>
<input type=text name='first_name' id='first_name'><span style='required'>*</span>
</div>
<div class='fields'>Last Name</div>
<input type=text name='last_name' id='last_name'><span style='required'>*</span>
</div>
<div class='fields'>Expiration</div>
<input type=text size=8 name='expiration' id='expiration'><span style='required'>*</span>(MMYY)
</div>
<div class='fields'>CVV Code</div>
<input type=text size=8 name='cvv' id='cvv'><span style='required'>*</span>
</div>
<div class='fields'>Credit Card Number</div>
<input type=text name='card' id='card'><span style='required'>*</span>
</div>
<hr>
<div class='buttons'></div>
<a onclick="readCard();" style="cursor:pointer; color:red;">Swipe Credit Card</a>
</div>
My knowledge of this kind of stuff is very poor. I have a basic little Credit Card Reader that plugs into my computer via USB. I am wanting to be able to swipe a credit card and have my website parse the information into the form fields that are above.
I have added an onclick=readCard(); event to a link below my form and when that is pushed java script is initiated to put focus on the Swiped Information field which will store the string of data from the magnetic stripe reader.
function readCard () {
document.getElementById('swiped').focus();
}
My thoughts would be that the employee would hit "Swipe Credit Card" which would make the Swiped Card Information field have focus and would fill that field with the string, then the javascript would break that information up into pieces and fill the form accordingly.
I have searched high and low to try and find a solution and the closest I could come was a tutorial that used asp.net as the language and I can't do that. Either PHP or JavaScript. Thanks in advance.
All I need to do is break that long string up into multiple and display the appropriate parts in the html form.
P.S. I'm not worried about form validation ATM, I will be taking care of that after I manage to make it fill the form fields! Thanks!
UPDATE:
I created a JSFiddle although the java script I put in doesn't appear to be working.
http://jsfiddle.net/r8FJX/
UPDATE:
As per the comments below, I have added an example of the data sent from my card reader to the computer. I went in and replaced every number in the string with randomly typed fake numbers and replaced my name with a fake one. (Sorry scammers!)
%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?
I am assuming this how the code above is laid out, I can't find any documentation:
%Bcardnumber^lastname/firstname^expDate?;cardnumber=expDate?
Option 1)
var card_data = "%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?"
var details1 = card_data.split("^");
var card_number = details1[0];
card_number = card_number.substring(2);
var names = details1[1].split("/");
var first_name = names[1];
var last_name = names[0];
var details2 = details1[2].split(";");
details2 = details2[1].split("=");
var exp_date = details2[1];
exp_date = exp_date.substring(0, exp_date.length - 1);
exp_date = exp_date.substring(2, 3) + "/" + exp_date.substring(0,1);
Option 2)
var pattern=new RegExp("^\%B(\d+)\^(\w+)\/(\w+)\^\d+\?;\d+=(\d\d)(\d\d)\d+$");
var match = pattern.exec(card_data);
card_number = match[1];
first_name = match[3];
last_name = match[2];
exp_date = match[5] + "/" + match[4];
Then Do:
document.getElementById("first_name").value = first_name;
document.getElementById("last_name").value = last_name;
document.getElementById("card").value = card_number;
document.getElementById("expiry").value = exp_date;
I had success with the follow for expiration date:
exp_date = exp_date.substring(2, 4) + "/" + exp_date.substring(1, 3);
Just For Future viewers like myself that was searching. The expiry needed to be adjusted. This will make the expiry look like... 10/18. Not 10/81 like I was getting...
Below shows the corrected formatted date of ex: 10/18 not 10/81 or 1/1
exp_date = exp_date.substring(2, 4) + "/" + exp_date.substring(0,2);
(For future people trying to parse USB credit card reader data)
There are two (sometimes 3) tracks of data, they are separated with ?. The expiry date is duplicated on the first track and the second track. If you want to read enough data to charge a credit card you can ignore the Track 2 data (everything from the ; onwards).
The CVC is not stored on the magnetic stripe data. You'll have to disable the CVC check in your payment processor. With Stripe you can do it at https://dashboard.stripe.com/radar/rules.
let parse = readerData => {
let card = readerData.match(/%B([0-9]+)\^([A-Z /.]+)\/([A-Z /.]*)\^([0-9]{2})([0-9]{2})/);
let lastName = card[2].trim();
// 'LASTNAME/FIRSTNAME.MR' is possible
let firstName = card[3].trim().split('.')[0];
let fullName = `${firstName} ${lastName}`;
return {
exp_month: card[5],
exp_year: card[4],
number: card[1],
name: fullName,
};
}
parse('%B6545461234613451^DOE/JOHN^21040000000000000000000?;this part does not matter')
// {exp_month: "04", exp_year: "21", number: "6545461234613451", name: "JOHN DOE"}
If you're using Stripe.js v2 you can pass the object returned by parse() directly to Stripe.card.createToken().
I've seen LASTNAME/FIRSTNAME MIDDLENAME in sample data, this code should turn that into FIRST MIDDLE LAST.
Read https://en.wikipedia.org/wiki/Magnetic_stripe_card#Financial_cards for more info.
I don't know if doing this is legal. Newer USB credit card readers encrypt the data they read (you have to send the data to their api (and pay them) to decrypt it).
If you're having issues with the regex try https://regex101.com/ for debugging it.

Javascript no jquery - Split a textareas single column list (unknown line count) & display in csv's of 150

I am trying to put together what I thought would be an easy solution for a friend.
He has a list of fan emails that he collects at gigs and off his website.
He sticks them in a single column file and saves them.
He needs to have them in comma delimited format containing 150 each, one single line.
He wants a "simple" local HTML form he can paste the list into and have the results displayed.
So, I started working on it but, it has proven past my ability.
So far I have some basics but, I really need help.
As you can see, I am really a beginner.
<html><head>
<script type="text/javascript">
function makeit(){
var enteredEmails = document.getElementById("emails").value;
var cnt = document.getElementById("breakcnt").value;
var mails = enteredEmails.toString();
var textareacnt = '1';
// Now I think I need to loop or create arrays of emails up to cnt //
csvmails = mails.splice(0,cnt)
// Then dynamically generate some textareas or boxes populated with a single comma delimited line of cnt" emails //
document.write("<textarea id=\"textareacnt\">" + csvmails + "</textarea>")
textareacnt++;
}
</script>
</head><body>
<form onsubmit="makeit();">
<textarea name="emails" id="emails" rows="10" cols="75"></textarea><br />
<input type="text" name="breakcnt" id="breakcnt"><br />
<input type="submit" name="submit" value="submit">
</form>
<textarea id="results"></textarea>
</body></html>
The textarera will have emails pasted into it like:
amail1#sometld.com
bmail1#sometld.com
cmail1#sometld.com
amail4#sometld.com
zmail10#sometld.com
... up to 6000 fan emails he has collected over the years
He needs the results to be:
amail1#sometld.com,bmail1#sometld.com,cmail1#sometld.com,amail4#sometld.com,zmail10#sometld.com
up to 150 emails long as I am sure the last chunk or array will not contain 150.
I cant get anything to work and I spent 6 hours on this so far.
Can someone please help? I feel like a complete idiot.
All you have to do is split the text into an array and then splice the array in a loop and join the slices you take out like this:
var emails= document.getElementById('emails').value.split(/\s+/), list="";
while(emails.length) {
list+=emails.splice(0,150).join(',')+"\n";
}
//list now has the result you are looking for
I have made an example of how to this here: http://jsfiddle.net/PfB42/2/
All you have to do is paste the emails into the text area and it will automatically change the format to the one you are looking for, and insert it to the <pre> area below the textarea
This should achieve what you want.
Don't be so hard on yourself mate :)
<html><head>
<script type="text/javascript">
function makeit(){
var enteredEmails = document.getElementById("emails").value;
var results = document.getElementById("results");
var index = 0;
var index2 = enteredEmails.indexOf("\n");
while(true) {
results.value += enteredEmails.substring(index, index2);
index = index2+1;
var index2 = enteredEmails.indexOf("\n", index+1);
//EOF
if(index2 == -1) {
results.value += ",";
results.value += enteredEmails.substring(index, enteredEmails.length);
break;
} else {
results.value += ",";
}
}
}
</script>
</head><body>
<form onsubmit="makeit();">
<textarea name="emails" id="emails" rows="10" cols="75"></textarea><br />
<input type="button" value="submit" onclick="makeit()">
</form>
<textarea id="results"></textarea>
</body></html>
Ouch, don't use that complicated scripts. Strings have functions exactly for that purpose:
var input = document.getElementById("emails").value;
input = input.replace(/\r\n/g, "\n"); // I hate this
var emails = input.split("\n"); // make an Array
var output = emails.join(","); // should we use ", " instead?
Of course you could put everyhing in one line ;-)

Count characters in paragraph using jQuery (*not* for input/textarea)

How do I work out in jQuery a character counter of a p/div tag?
Basically i want to show a different css value IF the character value >=50.
Been struggling for hours with it :)
Use
$("#myDiv").text().length;
var $div = $('#mydiv');
if($div.text().length >= 50) {
$div.addClass('class');
}
Put a "long" class on all div and p elements with more than 50 characters:
$("p, div").filter(function(){
return $(this).text().length >=50;
}).addClass('long');
If you don't know how much content you have, though, then presumably this content is generated dynamically by the server, right? And if this is the case, wouldn't it make more sense to have the server—which knows how much content it's plopping into these containers—add the class dynamically while generating the page to send? Why rely on jQuery?
Try this snippet it works even if the text's length was greater than 50 characters.
You can also calculate the character digits of the paragraph. (Excluding new lines and space bars)
window.setInterval(function() {
var txt = $("#txt").val();
var parLength = $("#data").text().length;
var charLength = txt.replace(/ /g, '').replace(/\n/g, '').length;
$("#data").text(txt);
$("#calcLength").text("The paragrapgh's length is: " + parLength);
$("#calcChar").text("The amount of characters is: " + charLength);
}, 10);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="txt" placeholder="Enter text here..." style="height:100px;width:250px"></textarea>
<p id="data"></p>
<p id="calcLength"></p>
<b>Note: even a space character is calculated through the length function</b>
<p id="calcChar"></p>
<b>Note: a space bar,and every empty line is trimmed in the second calculated value</b>
I have provided this example to make it simple for use and compatability.

Categories

Resources