CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-05-13
by julemand101

Original Post

Original - Posted on 2008-09-05
by John Rutherford



            
Present in both answers; Present only in the new answer; Present only in the old answer;

The issue here is that you don't specify any `else` clause which means that in case of both your if's being false, the `_msg` will never get any value. That is a problem since the default value for any variable in Dart is `null` and the type `String` means we are not allow to set this variable to `null`.
So this would be valid: ```dart String validateEmail(String value) { String _msg; RegExp regex = RegExp( r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'); if (value.isEmpty) { _msg = "Your username is required"; } else if (!regex.hasMatch(value)) { _msg = "Please provide a valid email address"; } else { _msg = 'Some text'; // throw Exception('Alternative, throw an error.'); } return _msg; } ```
(Yes, Dart is clever enough to recognise this pattern and will allow `String _msg;` in this case since it can safely ensure that `_msg` would always end up being given a value before we are using `_msg`.)
Using [regular expressions](http://en.wikipedia.org/wiki/Regular_expression) is probably the best way. You can see a bunch of tests [here](http://jsfiddle.net/ghvj4gy9/embedded/result,js/) (taken from [chromium](https://cs.chromium.org/chromium/src/third_party/blink/web_tests/fast/forms/resources/ValidityState-typeMismatch-email.js?q=ValidityState-typeMismatch-email.js&sq=package:chromium&dr))
```js const validateEmail = (email) => { return String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); }; ```
Here's the example of a regular expression that accepts unicode:
```js const re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; ```
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
Here's an example of the above in action:
```js const validateEmail = (email) => { return email.match( /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); };
const validate = () => { const $result = $('#result'); const email = $('#email').val(); $result.text('');
if (validateEmail(email)) { $result.text(email + ' is valid :)'); $result.css('color', 'green'); } else { $result.text(email + ' is not valid :('); $result.css('color', 'red'); } return false; }
$('#email').on('input', validate); ```
And this is the `html`:
```html <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="email">Enter an email address: </label> <input id="email" /> <h2 id="result"></h2> ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;