I'm currently trying to fill some fields in my forms. I'm doing a test where if extnReason and extnDt are null, I do nothing. But for some reason, it keeps entering the check and loading my fields with null, which I don't want.
function preloadFields() {
//these values are coming in as null
var extnReason = '<%=extnReason%>';
var extnDt = '<%=extnDt%>';
//set Extension Date blank on load
$('#extnDt').val("");
alert("reason ++++ " + extnReason);
alert("extnDt ++++ " + extnDt);
//it is entering these tests but I don't want them to
if(extnReason != null || extnReason != "null"){
console.log("entered reason");
$('#extnReason').val(extnReason);
}
if(extnDt != null || extnDt != "null") {
console.log("entered extnDt");
$('#extnDt').val(extnDt);
}
}
Any help would be greatly appreciated!
You'll need
if(extnReason != null && extnReason != "null")
instead of
if(extnReason != null || extnReason != "null")
Because if extnReason is 'null' the first condition 'null' != null would return true, so an OR check would evaluate to true and therefore enter the block of code which sets your value.
Same for the other if condition...
Alternative way of preloading your fields: you could also just set the value attribute of your input tag instead of using preloadFields? i.e. something like:
<input type="text" id="extnDt" value="${extnDt != null ? extnDt : ''}" />
Related
Good Day!
I created a custom validation on my SuiteCRM were it will validate the First Name, Last Name and Mobile number if it was already existing. But my problem is after the validation works the data won`t save or update.
Check my view.edit.php codes below:
$javascript = <<<'EOT'
<script type="text/javascript" language="JavaScript">
function customJavascriptDuplicateValidation(thisview)
{
var firstname = document.getElementById('first_name').value;
var lastname = document.getElementById('last_name').value;
var mobile = document.getElementById('mobile_number').value;
var birthday = document.getElementById('birthday').value;
var email = document.getElementById('email_c').value;
var location = document.getElementById('location_code').value;
var consent = document.getElementById('consent_timestamp_date').value;
var salutation = document.getElementById('salutation').value;
$.post('index.php?entryPoint=checkDuplicateCustomers', { first_name: firstname, last_name: lastname, mobile_number: mobile }, function(data)
{
if (data == 'exists') {
$('#error_firstname_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
$('#error_lastname_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
$('#error_mobile_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
return check_form('EditView');
}else if (data == 'unique'){
$('#error_firstname_msg').hidden;
$('#error_lastname_msg').hidden;
$('#error_mobile_msg').hidden;
if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
return check_form('EditView');
}else{
return check_form('EditView');
}
}else{
$('#error_firstname_msg').hidden;
$('#error_lastname_msg').hidden;
$('#error_mobile_msg').hidden;
return check_form('EditView');
}
});
}
</script>
EOT;
And I modify the save button at editviewdefs.php
$viewdefs['Accounts']['EditView']['templateMeta']['form']['buttons'][0] = array(
'customCode' => '<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById(\'EditView\'); _form.action.value=\'Save\'; if(customJavascriptDuplicateValidation(\'EditView\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE">',);
The Native Valition works when you click SAVE.
The custom validation works also, if you click SAVE:
After filling up the required field and inputting unique customers information. The data will not save.
My conclusion on this was on my custom javascript codes. Because if I put the codes below outside of the $.POST function the data will save, but the problem the custom duplicate validation will not work if all field in the condition is not null/empty.
if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
SUGAR.ajaxUI.showLoadingPanel();
return check_form('EditView');
}
Please disregard this one, I was able to fixed this by just adding this code below:
SUGAR.ajaxUI.showLoadingPanel();
var _form = document.getElementById('EditView');
_form.action.value='Save';
SUGAR.ajaxUI.submitForm(_form);
return check_form('EditView');
I want to check the value is not blank or one empty space so I wrote a code
var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value != " " && OccLocation.value != "") {
alert("not empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass"
/>
You can update your condition as below.
var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() == "") {
alert("empty");
}
If you want to get alert if OccLocation is not empty then :
var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() != "") {
alert("not empty");
}
Your condition is wrong.
You have to use == instead of !=.
If you use && then both condition should be true to return true, which is ultimately impossible at the same time in this case. Use || instead, this will be evaluated as true if any of the condition is true.
The condition should be:
if (OccLocation.value ==" " || OccLocation.value == "")
Even you can simplify the condition by using String.prototype.trim()
:
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
Try
if (OccLocation.value.trim() == "")
var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim()== ""){
alert ("empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass" />
You are checking that it is not empty, then alerting that it is empty. I think you mean to check that it is empty. Change your JS to the following:
var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value === " " || OccLocation.value === "")
{
alert ("empty");
}
Your code runs immediately, and the value="" sets it to empty.
Here, I set the value in the markup so it has some, thus it alerts.
var OccLocation = document.getElementById("HdnOccLocation");
console.log(OccLocation.value)
if (OccLocation.value != " " && OccLocation.value != "") {
alert("not empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="dd" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass"
/>
I have some JavaScript that is supposed to act as an example of how you can validate prompt box inputs.
User clicks button, enters a name, the JavaScript validates the input and displays an appropriate message. If the name is fine, it says it is a good name, if you enter a number/symbol it says invalid input (all good so far). However, when the user clicks "cancel" on the prompt box, the message displays "null" is a good name. I have tried to catch this but it doesn't seem to work. How can I make it display a message saying you did not enter a valid name when the user clicks "cancel"?
Here is the JS fiddle for it: http://jsfiddle.net/TurgidWizard/jzzsqu06/
html:
<button onclick="Validation()">Click Me</button>
<p id="vresult"></p>
Javascript:
function Validation() {
document.getElementById("vresult").innerHTML = "";
PetName = prompt("Please enter your favourite pet's name:", "");
var T = Test(PetName);
if (T == false | T == "null") {
document.getElementById("vresult").innerHTML = "You did not enter a valid name!";
} else {
document.getElementById("vresult").innerHTML = PetName + " is a lovely name, good choice!!";
}
}
function Test(str) {
return /^[a-zA-Z]+$/.test(str)
}
Notice how I tried to use "if (T == false | T == "null")" to capture "null" ready for the invalid message.
Your syntax is a bad here:
if (T == false | T == "null")
null shouldn't be a string, or is || not |.
You also want to be checking if PetName is null, not the result of the regex.
The line should look like this:
if (!T || !PetName)
Here's your fiddle: http://jsfiddle.net/jzzsqu06/1/
just check if str is null (as opposed to "null")
function Test(str) {
return (str !== null) && /^[a-zA-Z]+$/.test(str);
}
then simply check
if (T === false) {
prompt will return the entered value if OK or return is pressed, including empty strings "", and null if Cancel is pressed.
Incorporating this line in your if condition deals with the cancel option:
if (T != '' && T != null){
document.getElementById("vresult").innerHTML = "You did not enter a valid name!";
}
JS
if (isNaN(BDyear) == true || isNaN(BDmonth) == true ||
isNaN(BDday) == true || BDday.length != 2 ||
BDmonth.length != 2 || BDyear.length != 4)
{
document.getElementById("BDyear").value = ''
document.getElementById("BDmonth").value = ''
document.getElementById("BDday").value = ''
document.getElementById("bderror").style.display = "inline"
BDstate = false BDcheck = false
}
HTML
<tr>
<td>שנת לידה</td>
<td>
<input class="text" id="BDyear" maxlength="4" style="width:8%" /> / 
<input class="text" id="BDmonth" maxlength="2" style="width:5%" /> / 
<input class="text" id="BDday" maxlength="2" style="width:5%" />
<br />
<p id="bderror" style="position:absolute; top:70%; color:red; font:65% arial; display:none">תאריך לידה לא תקין</p>
<p id="bderroryoung" style="position:absolute; top:70%; color:red; font:65% arial; display:none">חובה להיות מעל גיל 13</p>
</td>
</tr>
the script part runs regardless whether i put in the inputs a number or words, with any length and i don't understand why is it running, but i'm suspecting it's the "isNaN" function that is not working correctly from different tries and setups. it's supposed to find out if the content entered is only a numric value that is in the proper length for dd/mm/yyyy and if it's all false it's supposed to leave everything as is and BDcheck var to be true so the next if statement will run
Any suggestions?
Make sure BDyear, BDday and BDmonth actually contain values; you may need to use document.getElementById("BDyear").value inside the isNaN functions. The value is also probably a string, so you may want to try first casting it to a number before checking isNaN like this: isNaN(Number(document.getElementById("BDyear").value)).
Also, isNaN returns a Boolean, so comparing it to true is redundant. You can just write it like if (isNaN(BDyear) || isNaN(BDmonth) || isNaN(BDday) || BDday.length != 2 || BDmonth.length != 2 || BDyear.length != 4), possibly with the changes I just suggested.
Ultimately, the code could look like this:
//assuming BDstate and BDcheck variables are already defined
var year = document.getElementById("BDyear");
var month = document.getElementById("BDmonth");
var day = document.getElementById("BDday");
if (isNaN(Number(year.value)) || isNaN(Number(month.value) || isNaN(Number(day.value)) || day.value.length != 2 || month.value.length != 2 || year.value.length != 4)
{
year.value = ''
month.value = ''
day.value = ''
document.getElementById("bderror").style.display = "inline"
BDstate = false
BDcheck = false
}
Also, instead of checking that the string length of month is 2 characters, you may want to check whether the numeric value is between a valid range. For example, a user would currently have to enter "06" (without an leading or trailing spaces, btw) in order for the check to succeed, whereas they could enter " 6 " if you were actually checking that the numeric value is within a valid range like (Number(month.value) >= 1 && Number(month.value) <= 12). The same goes for the day and year.
I have this form
<form class="form" method="post">
<input type="text" id="input_what" holder="what" />
<input type="text" id="input_where" holder="where" />
<input type="submit" value="submit" />
</form>
and this script to prevent submitting the form
$('.form').submit(function(e) {
var what = $('#input_what').val();
var where = $('#input_where').val()
if ( what == "what" || what =="" && where == "where" || where == "") {
e.preventDefault();
console.log('prevented empty search');
return false;
}
});
I know that my condition doesn't work, but i need it to work like this
IF (what == "what" OR what == "") AND (where == "where" OR where == "")
have a look at this fiddle to understand why http://jsfiddle.net/pK35e/
the placeholder-script i´m using, needs me to not submit the form for the cases above
using placeholder="attribute" is no solution for me, so can anyone give me a hint how to set this if-condition ?
Uses parenthesis just like in the textual description you made :
if (( what == "what" || what =="") && (where == "where" || where == "")) {
Side remark : You might be interested, for future versions as it's not supported by IE9-, by the placeholder attribute which will make this simpler.
Try this
if ( (what == "what" || what =="") && (where == "where" || where == ""))
IF ((what == "what" ||what == "") &&(where == "where" ||where == ""))
Use parens. The && operator has a higher precedence than the || operator.
if ((what == "what" || what =="") && (where == "where" || where == ""))
http://en.m.wikipedia.org/wiki/Order_of_operations
I believe you need some parenthesis in order to get what you want:
if ( (what == "what" || what =="") && (where == "where" || where == ""))
This means that both
(what == "what" || what =="")
and
(where == "where" || where == "")
has to return true in order for the code within your if statement to be executed. This is actually quite close to your textual example.
--
Just for the understanding of all of this. Your old code would look like this with parenthesis:
if ( (what == "what") || (what =="" && where == "where") || (where == "")) {
Where again, just one of these would have to return true.