I have a field called price, now how to validate this field by java script? It will take only integer value and dot. It may take decimal point or not.
e.g 200,200.00 both are valid.
How shall I do that?
There are many ways to achieve this.
Without the use of any 3rd party libraries i would recommend using regular expressions.
A possible pattern used to validate price would be
\d[\d\,\.]+
here is a tutorial on using regular expressions to validate fields in java-script
Tutorial
Hope this helps
Related
I wanted to make a really easy JavaScript validation for the IBAN.
It is for a project on school what means the goal of the validation isn't to get a 100% good IBAN validation but something easy to get along with.
I tried to create my own:/^[A-Z]{2}+[0-9A-Z]*$/
But apparently it seems to disactivate all the Javascript in the same file.
What is the reason that this disactivates all my JavaScript, and what is a good validation?
The conditions of the validation (might it not be clear already):
The first two characters must be alphabetic and upper-case.
The other characters can be numeric and/or alphabetic.
The length doesn't have to be included because that is checked with another if-statement in my function.
That's because you're using two-quantifiers side-by-side
/^[A-Z]{2}+[0-9A-Z]*$/
// ^ Remove this. It means match the previous token one or more times
So, it would be /^[A-Z]{2}[0-9A-Z]*$/
I'm using form validator in order to validate some fields on my signup form. One of which is a telephone field for which I have prepared two regexes that will provide validation.
regexp: {
regexp:
>/(^01|^02|^071|^073|^074|^075|^076|^077|^078|^079)/,
/^((?!(012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}|5{6,}|6{6,}|7{6,}|8{6,}|9{6,})).)*$/
message: 'The phone number is not valid'
}
My issue is that only one of those two will work, not both. Any idea how I could modify this such that they both work?
This regex will work, but I'd still suggest maybe finding a different way or set of criteria to validate. It's up to you though.
/^(0(?:[12]|7[145789]))(?!012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}5{6,}|6{6,}|7{6,}|8{6,}|9{6,})/
This will ensure the number begins with the proper prefix and does not contain the sequences you've indicated.
You could combine the two like this:
/^(?=(01|02|071|073|074|075|076|077|078|079))((?!(012345|123456|234567|345678|456789|0{6,}|1{6,}|2{6,}|3{6,}|4{6,}|5{6,}|6{6,}|7{6,}|8{6,}|9{6,})).)*$/
Does seem like a kind of obfuscated way to perform some fairly simple checks to me, but to each their own.
I have an HTML form I've created that has a field that needs to take a dollar amount.
From what I've read, JavaScript doesn't recognize decimals as numeric components and the accepted practice is to take input in cents rather than deal with dollar amounts.
Problem is, the form I'm writing submits to a function that reads the form input and performs some actions with them and takes this dollar amount as text in the format ##.## and will reject any input not in this exact format.
What I'm wondering is: is it possible to create a text box that has takes 5 characters and sets the third character as a permanent decimal point?
If not, is there any way I can use JS validation to ensure that the input is in the proper format?
Thanks!
I think you are going to have to validate it. This post has some good regex for validation dollar amounts: Currency validation. It also has some good points for how to trim so that a user can't enter $412.234 or anything like that.
You cannot create, in HTML, a text box that has a particular character fixed in it. There is hardly any need either, since it is more natural for a user to type 12.34 (when specifying a sum of money) than to type 1234 and have a period inserted automatically.
You can specify the required format using the HTML5 pattern attribute. It is not supported by all browsers yet, but it does no harm when not supported, and it performs the checks even when JavaScript is disabled. You should add JavaScript checking of course, and in a simple case like this, it might be better to write the code directly instead of using any library:
<input pattern="\d?\d\.\d\d" maxlength=5 size=5 onchange="check(this)">
<script>
function check(elem) {
if(!elem.value.match(/^\d?\d\.\d\d$/)) {
alert('Error in data – use the format dd.dd (d = digit)');
}
}
</script>
The regular expression used accepts both dd.dd and d.dd (omit the ? to allow the first format only).
The example uses alert for simplicity; in a real page, you should use some less disruptive way of signaling the error to the user (typically, writing text to an area reserved for such messages), but the best way to do that depends on the design of the page as a whole.
(It might seem more logical to use <input type=number min=0 max=99.99 step=0.01 ...>, but this would raise serious localization issues, and browsers implement type=number rather poorly if at all. Most importantly, there is no guarantee that the data sent would conform to the format required. E.g., in Chrome, using the controls created by the browser, the number would be stepped from 0.09 to 0.1 and not 0.10, from 0.99 to 1 and not 1.00 etc.)
Is it possible to do client side validation in a localized web application environment?
I've only seen regular expressions written in English, can they be written for other languages? Would the regular expressions have to be changed based on the language chosen by an end user or is it possible to use just 1?
Are there any tools/frameworks to help with this?
Previous answer was good, but it's not clear to me that it answered the question. For that matter, I don't really understand the question. If you're asking whether JavaScript regular expressions are independent of language, then the answer is yes, they are just looking at characters in a string. But obviously the things you're looking for with those regular expressions (words, numbers, phone numbers, dates, etc.) would presumably vary with language and locale. So you may be able to construct a universal regex that works to validate all phone numbers, for example, but it's probably unlikely, and in any case there may be cases where a valid number in one context is invalid in another. You're better off to create language-specific regular expressions used for validation just as you would create language specific strings. Does that answer your question?
No. Please do not confuse validation for well-formedness. The former is a measure of conformity to a grammar definition and the later is a measure of conformity to a syntax requirement. Even if your regex was so extremely awesome as to account for all well-formedness conditions it is absent the context of structured definitions where the structure is recursive and reflective.
I have this C# regex:
^\s?((?<qty>\d+)\s?/)?\s?[$]?\s?(?<price>\d{0,2}(?:\.\d{1,2})?)\s?$
and use MVC's data validation at the client. JavaScript says this regex is invalid, although C# works perfectly fine. Any idea how to get it to work for both C# and JavaScript, since it doesn't seem possible to provide a separate JavaScript Regex in the Data Validation annotations?
The regex validates a quantity and price. 4/$2.69 for example.
Javascript does not support named reference (?<…>…). You need to use
^\s?((\d+)\s?/)?\s?[$]?\s?(\d{0,2}(?:\.\d{1,2})?)\s?$
and refer qty and price as 1 and 2 instead.
Remove the group names (<qty>).