Regular expression is not working for numbers in jsp java - javascript

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

Related

compare Two field if the field is in the content of other field using javascript after submit button clicked

Hi I am not a javascript expert thats why I will really appreciate any advice
I have a textfield named try where in I will input something
ex:
try value is
87
then I have another textfield named field11
field11 has a value of
777-a98;87-bx23;000-t88;245-k7
I wanted to compare try and field11 if try is found in the content of field 11 it will set the textfield named msg to 87-bx23 matched
msg value will be
87-bx23 matched
my code is like this but its not giving the desired output I know my comparison is wrong it just I dont know how
<script>
$(document).ready( function(){
$('#submit').click(function() {
if (document.getElementById('try').value != document.getElementById('field11').value)
{
alert('dont match!');
$("#msg").val ("dont match!");
}
else if (document.getElementById('try').value == document.getElementById('field11').value) {
}alert(document.getElementById('try').value + " exists");
$("#msg").val(document.getElementById('try').value + " exists");
});
});
</script>
I also try this but if I input 77 it saying it exist even not
<div id="requirement #2">
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
<script>
function StringSearch() {
var SearchTerm = document.getElementById('try').value;
var TextSearch = document.getElementById('field11').value;
if (SearchTerm.length > 0 && TextSearch.indexOf(SearchTerm) > -1) {
alert("Patient Exists");
} else {
alert("Patient Does not exist click add if you want to add the patient");
$("#msg").val(document.getElementById('try').value + " exists");
$("#t1").val("1");
}
}
</script>
document.getElementById('field11').value.match(new RegExp("(" + "87" + "-[a-z0-9]+);"))[1])
The Generated Regex when try value = 87:
/(87-[a-z0-9]+);/
So what is this monstrosity? We generate a Regex Expression, that looks for the try value followed by a dash, and one or more characters from a-z or 1-9, followed by a semicolon. String.match() is used to determine an array of matches, the array[1] is the first capture group (the part of the RegEx between the brackets), which is in this case 87-bx23
I have tried to rewrite your code to store variables for the elements and use a Regexp to do the search for the value:
<script>
$(document).ready( function(){
var tryElem = document.getElementById('try');
var field1 = document.getElementById('field11');
$('#submit').click(function() {
var regex = new Regexp(tryelem +'[^;]*');
var match = regex.exec(field.value);
if (match)
{
alert(match + " exists");
$("#mag").val(match + " exists");
}
else
{
alert('dont match!');
$("#msg").val ("dont match!");
}
});
});
</script>
The code does more or less the same as yours, except for the regex:
tryelem +'[^;]*'
It builds a regular expression form the the value of tryElem and then it searches forward and matches up to the first semi colon (zero or more characters).
Now the match will contain: '87-bx23'.
You can try this:
<input type="text" id="try"/>
<input type="text" id="field11" value="777-a98;87-bx23;000-t88;245-k7"/>
<input type="text" id="msg"/>
<input type="button" id="submit"/>
And js:
$(function(){
$("#submit").click(function(){
var Try=$("#try").val(),
f11=$("#field11").val(),
msg=$("#msg");
//but if you want search in number part only (before dash sign), uncomment line below and comment next line.
//var r=((";"+f11+";").match(new RegExp(";([^;-]*"+Try+".*?-.+?);"))||[])[1];
var r=((";"+f11+";").match(new RegExp(";([^;]*"+Try+"[^;]*);"))||[])[1];
msg.val(r||"don't match!!");
});
});
You can check or change both of them online

comma separated phone number validation

In the code below, I have a phone number field and what I want to achieve is make sure each number is 15 digits and also ensure that if multiple phone numbers are entered (comma separated) (see code to understand), they too are 15 digits.
$("#btn").on('click',function(){
var regrExpr = new RegExp("^(?=\S{10,}$)(?=.*\d{15},?).*");
//var regrExpr = new RegExp("\d{15}(?:,\d{15})*");
//var regrExpr = new RegExp("\d{10,15}(?:,\d{10,15})*");
//var regrExpr = new RegExp("^(\d{15}[,]{0,1})+$");
//var regrExpr = new RegExp("^\+\d{10,15}(,\+\d{10,15})*$");
//var regrExpr = new RegExp("^(?=\S{10,}$)(?=.*\d{15},?).*");
if (!regrExpr.test($("#txt").val()))
{
alert("Please Enter No");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel" id="txt" style="width:300px">
<button id = "btn">Check</button>
Regex'ically, I need something like this :
~^[0-15]{15}(?:,[0-15]{15})*$~
Found this here
But, it doesn't work in my case.
here is the working regular expression of your case.
^\+\d{10,15}(,\+\d{10,15})*$
I believe the best regex would be
^(?=\S{10,}$)(?=.*\d{15},?).*
check the demo at here
here
(?=\S{10,}$)
ensures that the string is atleast of length 10
(?=.*\d{15},?)
is responsible for matching if number is of 15 length and is comma seperated
Use /.../ notation to define regex:
$("#btn").on('click',function(){
var regrExpr = /^\d{15}(?:,\d{15})*^/;
if (!regrExpr.test($("#txt").val())) {
console.log("Invalid number, retry");
return false;
} else {
console.log("All correct")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel" id="txt" style="width:300px">
<button id = "btn">Check</button>
Finally i got a solution
Check this Answer
var pattern = /^(?:\s*\d{15}\s*(?:,|$))+$/;
if(pattern.test($("#txt").val())){
alert("Correct");
} else {
alert("Wrong");
}
``

Phone Number Javascript Validation

Hello I am having trouble with my javascript validation. I am trying to validate a Phone number which I want it to display only numbers and single spaces.
My Javascript code:
<script type="text/javascript">
function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;
if (!isNaN(phone).value)//if the entered phone number is not a number
{
alert("Please enter a valid phone number");
ret = false;//return false, form is not submitted
}
}
</script>
HTML/PHP Code:
echo '<form name="checkout_details" action="confirm.php" onsubmit="return validateForm()" method="post">';
echo '<font color="red">*</font> <b>Phone Number:</b> <input type="text" name="phone" id="phone"><br /><br />';
echo '<input type="submit" value="Purchase">';
</form>
Anyone help me out tell me what i'm doing wrong. Thanks.
This is incorrect:
if (!isNaN(phone).value)//if the entered phone number is not a number
That actually evaluates to if it IS a number (double negative makes a positive). Also, ret = false; should be return false;
To remove spaces from the input before checking if it is valid, see this answer.
Try this
function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;
if (!isNaN(x))
{
alert("Please enter a valid phone number");
return false;
}
else
return true;
}
Your function needs to return true or false. A false return will prevent your form from posting.
So change this:
ret = false;//return false, form is not submitted
to:
return false;//return false, form is not submitted
Additionally, you cannot rely on isNaN to validate this field. In addition to not taking into account any phone numbers with spaces or other characters, it will not work correctly in some cases, and in this case you are negating its return value (!isNaN(x)), which means it won't work at all unless you fix that. You are better off using a regular expression to validate this field.

how can i validate the real numbers using regular expressions in 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

checking space between text in javascript

I want to check the gap between two or more words, i have given as an input, in a text box. If there had any space, then i want to alert user that no space is allowing. I can check the existence of text simply using "if else" statement. But can't do the desired stuff in this way. My code is given below :
<script type="text/javascript">
function checkForm()
{
var cName=document.getElementById("cName").value;
var cEmail=document.getElementById("cEmail").value;
if(cName.length<1)
{
alert("Please enter both informations");
return false;
}
if(cEmail.length<1)
{
alert("Please enter your email");
return false;
}
else
{
return true;
}
}
Name : <input type="text" id="cName" name="cName"/>
<br/>
<br/>
Email : <input type="text" id="cEmail" name="cEmail"/>
<br/>
<br/>
<input type="submit" value="Go!"/>
</form>
Thankyou
Just use the match() method of strings. For example:
'Spaces here'.match(' ');
That returns true.
'Nospace'.match(' ');
That returns false.
So for what you want, just use something like this:
if(cName.match(' ')){
alert('Spaces found!');
return false;
}
Demo
your question not very clear , but i hope you want to count your words you can use the following code to split a text and by using the length property you count the word
var b = document.getElementById("cName").value;
var temp = new Array();
temp = b.split(' ');
var count= temp.length;
and if you want to validate your name field that should not use any space
if ( ^[A-Za-z]$.test(document.getElementById("cName").value) ) {
// your code;
}
if ( document.getElementById("cName").value.indexOf(' ') > 0 ) {
alert('space found');
}

Categories

Resources