473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Seek javascript validator

Is there a way to confirm that a string has valid Javascript syntax?

Either a client side or server side solution would be ok.

For a client side solution I'd like to confirm that, say :

var a = "function foo { var t='ok'; alert(t); }"

is valid, or for a server side solution that

String a = "function foo { var t='ok'; alert(t); }"

is valid.

Thanks
Jul 17 '05 #1
3 3616
GIMME wrote:
Is there a way to confirm that a string has valid Javascript syntax?

Either a client side or server side solution would be ok.

For a client side solution I'd like to confirm that, say :

var a = "function foo { var t='ok'; alert(t); }"

is valid, or for a server side solution that

String a = "function foo { var t='ok'; alert(t); }"


Neither is client-side or server-side.

The first vaguely resembles JavaScript, and the second vaguely resembles
Java.

Each has both client-side and server-side forms.

But does your crosspost mean that you really are looking for a solution
in either language?

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #2
Yes. Either language. Java or Javascript.

But the language can not be server-side Javascript (old netscape Livewire).
But does your crosspost mean that you really are looking for a solution
in either language?

Jul 17 '05 #3
rh
gi*******************@yahoo.com (GIMME) wrote:
Is there a way to confirm that a string has valid Javascript syntax?

Either a client side or server side solution would be ok.

For a client side solution I'd like to confirm that, say :

var a = "function foo { var t='ok'; alert(t); }"
The following sample code should provide a check for valid Javascript
syntax.

Note that it could be greatly simplified if you are able to assume the
browser supports try/catch. If not, and the browser (e.g., IE 4.0,
Navigator 3.0) supports window.onerror, it is used in an attempt to
give a slightly more graceful failure. In the latter case,
window.onerror could be further exploited to provide support for the
validation.

validate = null;
validateFunctionText =
' try {'
+ ' var f = new Function(str);'
+ ' return "Validated OK";'
+ ' }'
+ ' catch (e) {'
+ ' return "Validation failed: "+(e.description '
+ ' || e.message || "");'
+ ' }';

function catchError(msg) {
return ! alert("Failed to create validator: "+(msg || "")) ;
}

if (typeof window.onerror != "undefined") {
onerrorSave = window.onerror;
window.onerror = catchError;
validate = new Function("str", validateFunctionText);
window.onerror = onerrorSave;
}
else validate = new Function("str",validateFunctionText);

// Test

var a = "function foo { var t='ok'; alert(t); }" ;

if(validate) alert( validate(a)) ;
else alert("Unable to validate Javascript");

../rh

Thanks

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Safalra | last post by:
Frequently in ciwah people say 'but what about the users without JavaScript?', so I decided to do an experiment. It suggests 35% internet users do not have JavaScript turned on in their browsers....
3
by: GIMME | last post by:
Is there a way to confirm that a string has valid Javascript syntax? Either a client side or server side solution would be ok. For a client side solution I'd like to confirm that, say : var...
18
by: John D | last post by:
I have a web page that attempts to call a piece of Javascript, which attempts to transform an xml file and an xslt file into Html. The call is here (A) : <script language = "JavaScript" src =...
5
by: Lucian Sandor | last post by:
Hello everyone, While I'm a newbie here, I a not new to google, so please don't send me back, it would be useless. First of all I have to specify I am working on a Blogger.com template, therefore...
2
by: Dot net work | last post by:
Hello. Say I have a .net textbox that uses a .net regularexpressionvalidator. If the regular expression fails, is it possible to launch a small client side javascript function to do something,...
4
by: Ian Cox | last post by:
I have a web form that contains a Datagrid. This grid has a number of columns, one of which contains a text box and validator for that text box. Everything works fine, when I press the "Save"...
1
by: Sky Kim | last post by:
Hi, all. I got stuck...here.. Let me simplify my problem.. I added One TextBox, and Required Validator associated with the textbox. And, Submit button. When submit is clicked it should do...
1
by: MattB | last post by:
I have a page with dual controls for a person to enter their height and weight. The dual controls are for English and metric units, and Javascript onChange events handle populating the other...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.