RegEx in javascript. Allow only letters, comma and punctuation - javascript

I try to make a RegEx for validating a form in javascript. The RegEx should only allow letters comma and punctuation. For instance I have this string:
Hi, this is a test of RegEx. Does it work
I've tried the following
/^[A-Za-z0-9,. ]{3,50}$/;
But it doesn't seems to work. Solutions?
Thanks!
EDIT:
This is my code:
<script type="text/javascript">
var RE_SSN = /^[A-Za-z0-9,. ]{3,50}$/;
function checkSsn(ssn){
if (RE_SSN.test(ssn)) {
alert("OK");
javascript:addAppointment(document.forms[0])
} else {
alert("NO!");
}
}
</script>
<div id="r">
<label for="receipt">Receipt</label><input type="checkbox" name="receipt" value="1"/>
</div>
<input type="button" value="Post it" onclick="checkSsn(this.form.content.value);"/>

You might need to escape the "." as that is a special character in regex.
/^[A-Za-z0-9,\. ]{3,50}$/;
Actually probably not. Try using http://www.regextester.com/ - I was able to get it to work anyway. Can you show us the full code for how you're implementing this?

if you want no punctuation at the beginning of the field:
/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/
this also allows spaces (one scenario for this is last name - De La Hoya, or O'Doul)

Related

Regex to strip html tag with certain attribute

I have some invalidly-nested HTML like:
<form class="form1" method="get">
<div>
<input name="field1">
</form>
<form class="form2" method="get">
<input name="field1">
</form>
</div>
Yeah, it's a mess, don't ask. The invalid nesting is causing problems somewhere else. jQuery I think is expecting a closing </div>, and only finding it at the last one. It's then treating the second <form> tag as invalid, and also discarding the closing </form> immediately above it, and assuming everything between lines 1 and 9 are one form.
If I output these to the console:
$('.form1).html() - all of line 1 - 9
$('.form2).html() - undefined
So what I'm trying to do is treat the whole thing as a string, and use regex to strip out form2. I'm expecting a regex something like:
formText.replace(/(<form\b[^>]*>)[^<>]*(<\/form>)/gi, "");
but I'm not sure how to reference the specific form with class=form2.
There's also a problem with it being a multi-line string.
Update: added more detail, outlining why jQuery's remove() method isn't working. jQuery only thinks there's one form unfortunately.
Don't use regex to parse HTML. Since you're using jQuery, just use .remove():
$(function() {
$(".form2").remove();
});
JSFiddle
I ended up using:
formText = formText.replace(/(<form\b[^>]*form2+.*>[\s\S]+<\/form>)/gi, "");
The [\s\S] matches all characters including \n and \r to cover the newlines.
I could probably have made the part of the regex dealing with the class name more specific so I knew it was the class and not some other random form with a similar, but in practice it didn't matter (there was only one instance of the 2nd form, with a very specific class name).

Regex end in backslash

I'm using regex in javascript to validate a form. One of the form fields is a filepath so needs to end in a backslash.
Specifically, I'm using <input type="text" pattern="" /> and I want to fill out the Pattern attribute to validate it.
Now..
I understand you make a backslash literal by doubling up ie. \\
and I understand that you use the dollar ($) sign to find the end of the string.
So can anyone explain to me why $// and //$ don't work? And maybe give me an example of something that would work?
Thanks
I got it working if I match the entire input, like so .*\\$
Dropping the $ behaved ok too,
<form>
path: <input type="text" pattern=".*\\" title="ends in \">
</form>
(using Chrome 27)
You seem to be mixing up slash / with backslash \. A \\$ is different from a //$, and \\$ should work.

Regex to test at least one number,can be alphanumeric,and should allow some special character

I am writing a regex for zipcode for following requirements.
It must contain at-least 1 number;
It could be alphanumeric;
Only special character allowed would be -, space and/or #.
Though it seems very simple,but I couldn't write it successfully. I am newbie to regex and not having smooth time with regex.
Please suggest solution.
Something like this?
/^(?=.*\d)[a-zA-Z\d #-]+$/
Just Try With The Follwoing Example :
HTML Part :
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var zipcode_expression = /^(?=.*\d)[a-zA-Z\d # ,-]+$/;
if (zipcode_expression.test($.trim($('#zipcode').val())) == false){
alert('invalid zipcode');
}
});
</script>
<input type="text" name="zipcode" id="zipcode" value="jo h,n1#-">
I think this may help you to resolve your problem.

Restrict characters entered into a text_field

I have a text field where users enter a URL string, which cannot contain spaces or non-alphanumeric characters (if that's an accurate way of putting it).
Is there a way in Rails to restrict entry into the text_field itself so that spaces and characters like /":}{#$^# can be avoided?
Thanks a lot.
To clarify, the only characters that should be possible are letters and numbers.
The problem here is that URL strings can have slashes (/) and hash marks (#). So your regex is going to be quite complex to ensure the right portion of the field is filtered properly. But for plain character filtering, you can use simple regex to remove any non alpha-numeric characters.
Not sure about anything ruby-specific, but in straight javascript:
<html>
<body>
<form>
<input type="text" name="whatever" id="form-field" value="" />
</form>
</body>
<script type="text/javascript">
var oFormField = document.getElementById('form-field');
oFormField.onkeyup = function() {
oFormField.value = oFormField.value.replace(/[^a-zA-Z0-9]/, '');
}
</script>
</html>
You may use jQuery Tools Validator that uses HTML 5 tags to validate your forms.
This is a great way to validate your forms in an Unobscursive way without putting JS all over your forms :-).
Look at the "pattern" HTML 5 tag that allows you to validate a field against a Regexp.
http://flowplayer.org/tools/validator/index.html

Validating HTML text box with javascript and regex

The below code works to only allow alphanumerics and spaces. However, I would like to also allow an accented character (Ã). How should the regex be modified?
Thanks
<html>
<head>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
$(function() {
$("#sub").bind("click",
function() {
$('#addText').val($('#addText').val().replace(new RegExp("[^a-zA-Z0-9 ]","g"), ''));
});
});
</script>
</head><body>
<div>Enter Text:</div>
<input id="addText" type=text/>
<input id="sub" type="button" value="Submit" />
</body></html>
If you only care about Latin-1 (Western European) letters, this should work:
[A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff]
For other scripts (eg: Greek, Cyrillic, Thai letters, CJK characters, etc.) things get much more complicated, and it becomes safer to just forbid things like control characters, rather than trying to keep track of which characters are "letters".
If you just want to add this one character .. just add it in the regex
[^a-zA-Z0-9Ã ]
Keep in mind though that there might be complications as mentioned below so do follow the suggestion by Laurence Gonsalves
http://unicode.org/reports/tr18/
http://www.regular-expressions.info/unicode.html

Categories

Resources