how can i validate the real numbers using regular expressions in javascript - javascript

Here is my code i have tried this but i didn't get the proper output. I want to give input like 10.00,100.00,1,000.00,10,000.00
How to validate this
<input type="text" name="depo_amt" id="depo_amt" maxlength="70" placeholder="10.00"onclick="formatNumber()"/>`
function formatNumber()
{
var regex = ^[0-9]\d{0,9}(\.\d{1,2})?%?$;
if (depo_amount.value=='regex')
{
alert("Number is valid");
return false;
}
else
{
alert("Number is not valid");
}
}

this regex will do that for you
^[1-9]\d{0,2}(,\d{3})*\.\d{2}$
http://regex101.com/r/uY3fS5

this will work hopefully..
please check and tell if anyone finds any descipancy
^[1-9]\d{0,2}(?:,\d\d\d)*\.\d{2}$
http://regex101.com/r/dS1zZ0

Related

How can I validate user input (characters and number) with javascript?

From this example
I tried to validate user input when a button was clicked.
$("#check_data").click(function () {
var $userInput = $('#user_input'); //from HTML input box id="user_input"
var pattern = " /*My user input always be like "AB1234567"*/ ";
if ($userInput.val() == '' || !pattern.test($userInput.val())) {
alert('Please enter a valid code.');
return false;
}
});
My user input input always be like "AB1234567" with this exact same characters but different 7 digits number.
I'm new to Javascript and Jquery, if you have any better way to validate user input, please suggest it.
Thank you.
You can use below regex Expression to check
/[A-Za-z0-9]/g
Your code could be like this
var _pattern=/[A-Za-z0-9]/g
if($userInput.val().match(_pattern))
{
//your code goes here..
}
Thanks
You can use the below pattern to check
/^AB\d{7}$/
You can change code to
var pattern = '/^AB\d{7}$/';
if ($userInput.val() == '' || !pattern.test($userInput.val()))
{
alert('Please enter a valid code.');
return false;
}
\d{7} matches 7 digits in the range [0-9]
You can follow below code for this:
if ($userInput.val().match(/[^a-zA-Z0-9 ]/g))
{
// it is a valid value.
} else {
// show error here
}
Hope it helps you.
Try this one.
$("#check_data").click(function(){
var $userInput = $('#user_input').val();
var pattern = /^AB[0-9]{7}?/g;
if(!$userInput.match(pattern)){
alert('Please enter a valid code.');
return false;
}
});

Regular expression is not working for numbers in jsp java

what I want is If the enter text box value is in format or not (1-10 number hyphen number) i did this for one pattern matching /^[\d]+([-][\d]+)?$/g but it allows only 0-9(single digits) i not taking 11-20(double digits) I need to allow any type digits like 10-11 and how can i do this
var txt = document.getElementById("txtShareCount").value;
if(txt.match(/^[\d]+([-][\d]+)?$/g)){
return true;
}
else if(!txt.match( /^[\d]+([-][\d]+)?$/g)){
alert("Pleae Enter Share count this(1-10) format");
document.getElementById("txtShareCount").focus();
return false;
}
else{return true;}
Here i'm posting the OP post as executable snippet to show his code is working perfect . i said in comment he didn't accept that . so only i'm posting this snippet .
1) Additionally i applied only trim()
$('#ss').click(function(){
var txt = $("#txtShareCount").val();
if(txt.trim().match(/^[\d]+([-][\d]+)?$/g)){
alert("hi valid format ");
}
else if(!txt.trim().match(/^[\d]+([-][\d]+)?$/g)){
alert("Pleae Enter Share count this(1-10) format");
$("#txtShareCount").focus();
return false;
}
else{
alert("hi valid format ");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inputt"><input type="text" id="txtShareCount" name="myanswer" class="myinput"></div>
<input type="button" id="ss" value="submit" >
The regex ^[\d]+([-][\d]+)?$ accomplishes what you need

Unable to validate properly in jquery

I'm validating an input from a user in jquery. If the input is empty, false is returned and jquery code doesn't run and if it contains some text the jquery code runs.
Here is an example-
function sendm() {
var valid;
valid = sendmval();
if (valid) {
//jquery code
}
}
function sendmval() {
var valid = true;
if (!$('#message').val()) {
valid = false;
} else {}
return valid;
}
This works fine. However the problem occurs when user inputs blank spaces only and thus results in running of jquery code even on blank input. How can I prevent this ?
Since spaces count as character so you have to use $.trim() of Jquery like below:-
if (!$.trim($('#message').val())) {
valid = false;
}
For more reference:-
https://api.jquery.com/jQuery.trim/
Since space is also a character, simple use .trim() function of Javascript strings to remove blank space in the beginning and end. Then proceed with your check as usual.
Note: I have changed:
if (!$('#message').val())
to
if (!$('#message').val().trim())
See full working code test:
function sendm() {
var valid;
valid = sendmval();
if (valid) {
alert("valid & sendm");
}
}
function sendmval() {
var valid = true;
if (!$('#message').val().trim()) {
valid = false;
} else {}
return valid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="message">
<button onclick="sendm()">CHECK</button>

Only numbers function

I have a javascript function to let the user enter only numbers. I need this function to let me enter "," and ";". How do I do this? Under the my function.
function SomenteNumero(e){
var tecla=(window.event)?event.keyCode:e.which;
if((tecla>47 && tecla<58)) return true;
else{
if (tecla==8 || tecla==0) return true;
else return false;
}
}
Thanks.
Please do not try to validate input using ASCII values. It is prone to errors and probably does not reliably determine what you are actually trying to. This is probably closer to something you need:
function isNumber(str) {
return /^[0-9,;]+$/.test(str);
}
Keep in mind that this example is probably not comprehensive. Your specifications would need to be a lot more detailed for it to fully suit your needs.
You need define on object. Try
`<script>
function NumberBox(e){
var _define=(window.event)?event.keyCode:e.which;
if((_define>47 && _define<58)) return true;
else{
if (_define==8 || _define==0) return true;
else return false;
}
}
</script>`
Then
<input name="Name" type="text" id="Name" size="15" maxlength="30" onkeypress="return NumberBox(event);">

Javascript phone number validation

I need to validate a phone number in javascript.
The requirements are:
they should be 10 digits, no comma, no
dashes, just the numbers, and not the
1+ in front
this is what I wrote so far
function validatePhone(field,alerttxt) {
with (field) {
if(value.length > 10) {
alert(alerttext);
return false;
}
for(i = 0; i < value.length; i++) {
if(parseInt(value[i]) == NaN) {
alert(alerttxt);
return false;
}
}
return true;
}
}
function validateForm(thisform) {
if (validatePhone(phone,"Invalid phone number")==false) {
phone.focus();
return false;
}
}
}
<form action="post.php" method="post" id="contactform" onsubmit="return validateForm(this)">
<ol>
<label for="phone">Your phone <span class="red"></span></label>
<input id="phone" name="phone" class="text" />
</li>
</ol>
</form>
but, obviously it doesn't work.
How can I write the validatePhone() function so that it works?
phone = phone.replace(/[^0-9]/g, '');
if(phone.length != 10) {
alert("not 10 digits");
} else {
alert("yep, its 10 digits");
}
This will validate and/or correct based on your requirements, removing all non-digits.
Google's libphonenumber is very helpful for validation and formatting of phone numbers all over the world. It is easier, less cryptic, and more robust than using a RegEx, and it comes in JavaScript, Ruby, Python, C#, PHP, and Objective-C flavors.
You could use Regular Expressions:
function validatePhone(field, alerttext) {
if (field.match(/^\d{10}/)) {
return true;
}
alert(alerttext);
return false;
}
Code to except only numbers braces and dashes
function DoValidatePhone() {
var frm = document.forms['editMemberForm'];
var stripped = frm.contact.value;
var isGoodMatch = stripped.match(/^[0-9\s(-)]*$/);
if (!isGoodMatch) {
alert("The Emergency Contact number contains invalid characters." + stripped);
return false;
}
}
Fixed function:
function validateForm(thisform) {
if (validatePhone(thisform.phone,"Invalid phone number")==false) {
thisform.phone.focus();
return false;
}
return true;
}
Add the pattern in which format you want directly in input tag.
<input id="phone" name="phone" pattern="\d{10}" class="text" />

Categories

Resources