JavaScript validation on name field not working - javascript

I am trying to make my form validation work but I somehow can't make my name field to validate properly. When I write names in the field it will say that I need to write a proper name. What am I doing wrong? HTML: http://pastebin.com/EfNSkQkS JavaScript: http://pastebin.com/c6iUCYvr

You simply forgot to add the + sign at the end of your letters RegExp. i. e. var reg_letters = /^[A-ZÆØÅa-zæøå]$/; should be var reg_letters = /^[A-ZÆØÅa-zæøå]+$/;

It's a problem with your regex expression in your javascript code var reg_letters = /^[A-ZÆØÅa-zæøå]$/; I don't know what other characters you want to allow
But when i changed it this -->var reg_letters = /^[a-zA-Z]*$/; it works(this regex expression is for English Characters only.
This is a plunker link to test it, just change them (var reg-letters) to test. plunkr link
*Note i removed the action and method from the form, so plunkr doesn't throw errors.

Related

Regular Expression to validate if user has provided the correct URI format or not

I am trying to write a regular expression or regex for my HTML input field with text type. The user should enter the URI in the input field. The URI would look something like this: urn:URNNamespace:**:class:ObjClassid.
Some of the examples of valid URI are as follows:
urn:abc:testing:pqrs:1234556.1244
urn:wxyz:testing123:abc:1234556.*
urn:global:standard:value:myvalue
I was trying to check only if the initial characters are URN using the expression ^(urn):// and check if the string contains the character : in it.
I just want to make sure that user enters valid URN similar to the one that's provided. Is there any better way I can use and achieve this?
I would recommend a really great tool called Regulex that helps you build regular expressions.
I tried to create the described pattern and ended up with:
^urn:\w+:\w+:\w+:\d+(\.\d+)?$
You can try and change it here

validation in javascript for escape sequence

I have textBox which accept alphabeti want to validate the textBox contain proper Drive Path or not using javascript
ex:Suppose Textbox contain 'D:\' then it's valid or else it's invalid...I need to check textbox contain ':\' or not after alphabet
plz help me
try to use javascript method "replace" http://www.w3schools.com/jsref/jsref_replace.asp to remove unwanted contents.
You can use following regex
/^(\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?$/

JavaScript Regex expression to extract data from html comment

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:
<!-- sid=FFKK12H1 -->
And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!
EDIT:
I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?
The regular expression would be: /<!-- sid=(.+?) -->/i:
var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);
var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);
The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.
var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');
Example
http://jsfiddle.net/YTdKQ/

Validation of a mathematical formula based on user input

I know that SO is not a code generator, but I break my head and I'll got mad with this RegExp.
I've <input /> type text, in a HTML <form />. The input is automatically filled when the user double-click on elements in a specific list.
This event will generate string like "[text:number]" or "[text:number:text]", and place it at the cursor position in my <input /> field.
The first goal of this process is to construct a mathematic formula structure. I mean, the generated strings between brackets will insert elements, then I want to allow the user to put only numbers and operators.
I've tried to bind the keydown event, and test the char with String.fromCharCode(e.which); but for the keys "+" or "-" (and other operators) this function returns alphabeticals chars. Without success.
Then, I've finally decided to use the keyup event, then use a RegExp to replace the <input /> value.
$("#inputID").keyup(function(){
var formule = $(this).val();
var valid_formule = formule.replace(CRAZY_REGEXP,'');
$(this).val(valid_formule);
});
So, my question is as follows :
How construct a javascript RegExp, to remove all chars which are not between brackets, and which are differents of ()+-*/,. and numbers.
An example :
"a[dse:1]a+a[dse:5]a+a[cat:5:sum]a+(a10a/5,5)!"
will become
"[dse:1]+[dse:5]-[cat:5:sum]+(10/5,5)"
I'm open to another way to achieve my goal if you have some ideas.
Thanks !
You may try something like this:
var re = /[^\]\d\(\)+\-*\/,.]+(?=[^\[\]\(\)]*(?:\[|\(|$))/g;
$("#inputID").keyup(function(){
this.value = this.value.replace(re, "");
});
Keep in mind, though, that you have to be sure that the parenthetical structure is coherent with your syntax.
Advice: use RegExr to test your regular expressions, but remember that it's more powerful than Javascript regex support.

Checking input values for special symbols

I am having two text fields in my form. The data to be entered in the field are Name and City respectively. I want to check that the user has not entered any special symbols like !,#,#........ i.e, the only thing user should enter must be belonging to a-z,A-Z, though the user can enter Underscore(_), but no numbers, no special symbols.
I want to check this using JavaScript, how can this be achieved.
Thanks in advance.
A classic problem that's usually solved with the help of regular expressions.
var myString = "London";
if (myString.match(/^[a-zA-Z_]+$/)) {
// Success
}
If you want to allow spaces, like for New York, change the pattern to /^[a-zA-Z_\s]+$/.

Categories