Javascript contains statement - javascript

I'm trying check a field in a database to see if it does not contain either "UK_CONTACTS or a blank. If it is either of these conditions I want to copy the that field to another field. I am very very new at this and have come up with the following and have written in text "does not contain" as I don't know the correct syntax for javascript.
function getdbasename(){
var dbasedata = document.forms[0]._dbase_name.value;
}
If (dbasedata does not contain "UK_CONTACTS" || dbasedata does not contain " ") {
_area.value = _dbase_name.value;
}
Probably miles out but it's my best shot.

I think you want indexOf(). If dbasedata.indexOf(someString) is anything but -1, it contains someString.

You'll need the indexOf('what you need to check for') function on the string you want to check.
E.g. (field.value.indexOf('myvalue') > 0) will be true if field.value contains the term 'myvalue'

Related

how to use regex with user input variable

I am getting an array of objects using JSON and then my goal is to let the user search for a specific login. For this I want them to be able to type a letter and check each object login, if the letter is contained I want to display it.
In order to achieve this I worked on the following code:
var i;
var out="";
var exp=/d/g;
var result = " ";
for(i=0;i<users.length;i++){
result= exp.test(users[i].login);
if(result){
out+= users[i].login+ " ";
}
}
It works fine if I write the regex (in this case d) but once I try putting a variable inside the regex it wont work. How do I create a regex that will take the users input and work with the test function to perform the same task? Or idk if there is a better/more elegant solution for this. I know there are different regex questions already but I didn't find one that helped me.
Appreciate the help!
You're testing a literal string - that string is passed in by the user but it's still literal, not a regex.
So you should try:
if( users[i].login.indexOf(userInput) > -1)
This will pass if the given input is in the searched string.

How to compare string variable using JavaScript

I am trying to compare the variable using javascipt:
response value: ""test#gmail.com""
response value i am getting it from server.
var str1="test#gmail.com"
var str2 =response;
if(str1===str2)
{
//
}
However not getting the proper result.
any idea on how to compare them ?
There are a few ways to achieve your goal:
1) You can remove all " from the response when doing your equality check:
if(str1===str2.replace(/['"]+/g, ''))
{
//
}
2) Change your server code to not include ". Doing so, would mean that your Javascript will not need to change.
3) Last option, add " to your str1:
var str1='"test#gmail.com"'
var str2 =response;
if(str1===str2)
{
//
}
Obviously I don't know enough about your requirements to tell you which one you should do, but my suggestion would be choice #2 because I think it's strange to return an email address wrapped in quotes, otherwise I would recommend #1.
You are trying to compare '""test#gmail.com""' with 'test#gmail.com'. They would never be equal.
Actually ""test#gmail.com"" is not a valid string. It might have been represented as '""test#gmail.com""' and "test#gmail.com" is a valid string (Same as 'test#gmail.com').

Using regex in javascript

I cannot get to work the following example of Regex in JavaScript. Regex is valid, was tested on some webs testing Regex expression.
I want it to check if input is in format: xxx,xxx,xxx.
It is alerting wrong input all the time. Thanks for any help.
var re = /[0-9a-zA-Z]+(,[0-9a-zA-Z]+)*/;
var toValidation = document.getElementsByName("txtSerial").value;
alert(toValidation);
if(!re.test(toValidation))
return true;
else
{
alert("Please insert valid text.");
return false;
}
document.getElementsByName("txtSerial") will return all elements by that name (node collection). Node collections do not have an attribute named value, thus, .value will be undefined (as can be seen by your alert).
Depending on your markup, you will want to use
document.getElementById("txtSerial")
or
document.getElementsByName("txtSerial")[0]
(although the last one is certainly not ideal).

Can i use /^USA$/ for a exact match? is it safe? any better idea?

I have my_string of my_text_field (but its hidden), for example, FRANCE USA_ILANDS GERMANY (space between these each country / words.....actually, all these country names are NAMES of text fields/drop-downs/check boxes on my_form) like that i am concatenating around 200 countrries, well.
Now, i want to search for a match in my_string, say for example, am looking to search for a match of USA, so i put the below JS,
var myCountryName = /USA/;
var returnValue = my_text_field.search(myCountryName);
if(returnValue != -1){; // This Country/field/object is found in the my_text_field, hence greyed out with readOnly
this.ui.oneOfChild.border.fill.color.value = "192,192,192";
this.access = "readOnly";
};
I am expecting the return value should be false.
But, its coming as true!
https://stackoverflow.com/questions/447250/matching-exact-string-with-j avascript
Am following the first suggested option with place holders like, var r = /^a$/ its working fone for me
Pls. let me know is this is safe? ok? recommendabale? or Any other help to find out exact match word?
Thank you
Use \b to check for word boundaries on each side.
/\bUSA\b/
If you want to know if USA exists then why not use:
my_text_field = ' '+my_text_field+' ';
my_text_field.indexOf(' '+myCountryName+' ');
Unless I'm missing something.

javascript regex help

i am trying to validate if a certain company was already picked for an application. the companyList format is:
60,261,420 ( a list of companyID)
I used
cID = $('#coName').val().split('::')[1];
to get the id only.
I am calling this function by passing say 60:
findCompany = function(value) {
var v = /^.+60,261,420$/.test(value);
alert(v);
}
when I pass the exact same string, i get false. any help?
Well if your company list is a list of numeric IDs like that, you need to make the resulting regular expression actually be the correct expression — if that's even the way you want to do it.
Another option is to just make an array, and then test for the value being in the array.
As a regex, though, what you could do is this:
var companyList = [<cfoutput> whatever </cfoutput>]; // get company ID list as an array of numbers
var companyRegex = new RegExp("^(?:" + companyList.join('|') + ")$");
Then you can say:
function findCompany(id) {
if (companyRegex.test(id)) alert(id + " is already in the list!");
}
Why not split the string into an array, like you did for your testing, iterate over the list and check if it's in?
A regexp just for that is balls, overhead and slower. A lot.
Anyway, for your specific question:
You’re checking the string "60" for /^.+60,261,420$/.
.+60 will obviously not match because you require at least one character before the 60. The commas also evaluate and are not in your String.
I don’t quite get where your regexp comes from.
Were you looking for a regexp to OR them a hard-coded list of IDs?
Code for splitting it and checking the array of IDs:
findCompany = function(value) {
$('#coName').val().split('::').each(function(val){
if(val == value) return true;
});
return false;
}

Categories