Javascript - Check if string have a special format - javascript

I have a text field and the user should insert there numbers seperated with a comma. For example:
1,2,3,4,50,100
How can I check now if the value of the text field have this format?

You can verify your input using regex:
/^\d+(,\d+)*$/.test(value)

Related

Add random value on specific field selenium webdriver

I'm pretty new to Selenium and Java just wanted to know if there's way I can randomly add a value in a specific field, lets say in "Case ID" field I want to generate a random 8 alphanumeric value, below is the xpath of the field I'm referring to:
driver.findElements(By.xpath(".//*[#id='case_id']")).size() != 0) {
Thanks in Advance,
P
Generate a random alphanumeric string
import java.util.UUID
UUID.randomUUID().toString();
randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. ex syntax :046b6c7f-0b8a-43b9-b35d-6489e6daee91
In your case you want eight digit alphanumeric String so just split the string
String arr[]=UUID.randomUUID().toString().split("-");
System.out.print(arr[0]);//046b6c7f
Assuming that case id is an input text box just use sendkeys to enter the text into the text box
driver.findElements(By.xpath(".//*[#id='case_id']")).sendKeys(arr[0]);
Hope this helps you.Kindly get back if i misunderstood ur question or if you need any further help

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\\]+)+))(\\)?$/

How to remove comma in the input text form using JS before it will be submitted to the server?

I have a form that when submitted to the server will contain commas in the query string values, for example:
test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000
Is there a way to remove this comma using JS just after the user clicks the form but before it will be submitted to the server? I have this approach here: How replace query string values using jQuery?
But that will only change the text to integer but in the URL itself. I also tried using input number attribute in HTML 5 but I cannot figure out how to use a comma by default to show it on the form: How to force to display a number with a comma with input type number?
I would appreciate any tips. Thanks.
UPDATE
Final working solution:
$('.my_form').submit(function() {
var x=$('#input_text_field_one').val();
x=x.replace(/,/g,'');
x=parseInt(x,10);
$('#input_text_field_one').val(x);
return true;
});
input_text_field_one is the input text form containing a number with commas.
.my_form is the class name for the form. After form submit the above code replace the numbers with commas to without commas. So finally, the numbers are submitted as number. :)
I think you want something like this:-
form.onSubmit = function(){
form.action = form.action.replace(",", "");
return true;
}

Best way to mask textbox control to allow string value in a pair of 3 character using javascript

I need to mask a textbox control to allow string value in a pair of 3 character using java script.
Eg:- I have a Text box control zip code in which when a user gives input then it should automatically pair them in a pair of 3 character (ABC#123), and total length of input is 6 Max.
Is it possible then how??
var text="ABC123";
//you can add any validation if you require like to check the length of string
//if(text.length<=6)
var output=text.substring(0,3)+" "+text.substring(3);
check this

regex - date format OR not specified/empty

I have a regex problem that I need some advice on. I am using a jquery plugin that validates input fields on a JSP. I am using the date validation. It all works fine but this field is not required and is not marked as required. When the user hits submit it shows a warning on the date input field since the empty string does not match the validation of a date. I need some way to say that nothing or a date is valid in a regex.
Here is the regex for the date; is there any way to put "or empty" into this also:
"date":{
"regex":"/^[0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4}$/",
"alertText":"* Invalid date, must be in MM-DD-YYYY format"},
Thanks in advance
Try:
/^([0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4})?$/
It will then match when a valid date is supplied, or the field is left blank.
(^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$|^$)

Categories